InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
PHP
Finding the Current Date and Time in PHP
PHP

Finding the Current Date and Time in PHP

InfinityCoder November 17, 2016

You want to know what the time or date is.

Use date() for a formatted time string, as in Example 3-2.

Example 3-2. Finding the current date and time

1
print date('r');

It obviously depends on the time and date the code is run, but Example 3-2 prints something like:

1
Fri, 01 Feb 2013 14:23:33 -0500

Or, use a DateTime object. Its format() method works just like the date() function:

1
2
$when = new DateTime();
print $when->format('r');

Use getdate() or localtime() if you want time parts. Example 3-3 shows how these functions work.

Example 3-3. Finding time parts

1
2
3
4
$now_1 = getdate();
$now_2 = localtime();
print "{$now_1['hours']}:{$now_1['minutes']}:{$now_1['seconds']}\n";
print "$now_2[2]:$now_2[1]:$now_2[0]";

Example 3-3 prints:

1
2
18:23:45
18:23:45

The function date() (and the DateTime object) can produce a variety of formatted time and date strings.

Both localtime() and getdate(), on the other hand, return arrays whose elements are the different pieces of the specified date and time.

The associative array getdate() returns the key/value pairs listed in Table 3-1.

Table 3-1. Return array from getdate()

Key  Value
seconds

minutes

hours

mday

wday

mon

year

yday

weekday

month

0

Seconds

Minutes

Hours

Day of the month

Day of the week, numeric (Sunday is 0, Saturday is 6)

Month, numeric

Year, numeric (4 digits)

Day of the year, numeric (e.g., 299)

Day of the week, textual, full (e.g., “Friday”)

Month, textual, full (e.g., “January”)

Seconds since epoch (what time() returns)

 

Example 3-4 shows how to use getdate() to print out the month, day, and year.

Example 3-4. Finding the month, day, and year

1
2
$a = getdate();
printf('%s %d, %d',$a['month'],$a['mday'],$a['year']);

Example 3-4 prints:

1
February 4, 2013

Pass getdate() an epoch timestamp as an argument to make the returned array the appropriate values for local time at that timestamp.

The month, day, and year at epoch timestamp 163727100 is shown in Example 3-5.

Example 3-5. getdate() with a specific timestamp

1
2
$a = getdate(163727100);
printf('%s %d, %d',$a['month'],$a['mday'],$a['year']);

Example 3-5 prints:

1
March 10, 1975

The function localtime() also returns an array of time and date parts. It also takes anepoch timestamp as an optional first argument, as well as a boolean as an optional second argument.

If that second argument is true, localtime() returns an associative array instead of a numerically indexed array.

The keys of that array are the same as the members of the tm_struct structure that the C function localtime() returns, as shown in Table 3-2.

Table 3-2. Return array from localtime()

Numeric position Key Value
0

1

2

3

4

5

6

7

8

tm_sec

tm_min

tm_hour

tm_mday

tm_mon

tm_year

tm_wday

tm_yday

tm_isdst

Second

Minutes

Hour

Day of the month

Month of the year (January is 0)

Years since 1900

Day of the week (Sunday is 0)

Day of the year

Is daylight saving time in effect?

Example 3-6 shows how to use localtime() to print out today’s date in month/day/ year format.

Example 3-6. Using localtime()

1
2
3
4
$a = localtime();
$a[4] += 1;
$a[5] += 1900;
print "$a[4]/$a[3]/$a[5]";

Example 3-6 prints:

1
2/4/2013

Similarly, the year is incremented by 1900 because localtime() starts counting years with 0 for 1900.

The functions getdate() and localtime() both use the same internal implementation to generate the returned date and time parts.

They differ only in the format of the returned arrays and in some of the information they return. (For example, local time() includes whether DST is in effect at the specified time.)

The time zone that getdate() and localtime() use for their calculations is the currently active one, as set by the date.timezone configuration variable or a call to date_de fault_timezone_set().

 

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Reading Passwords in PHP
You need to read a string from the command line …

Reading Passwords in PHP

Avoiding Cross-Site Scripting in PHP
You need to safely avoid cross-site scripting (XSS) attacks in …

Avoiding Cross-Site Scripting in PHP

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap