PHP Dates and Times Finding the Current Date and Time - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript PHP Dates and Times Finding the Current Date and Time - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Saturday, May 11, 2019

PHP Dates and Times Finding the Current Date and Time

PHP Dates and Times





Finding the Current Date and Time

Problem

You want to know what the time or date is.


Solution

Example   Finding the current date and time

            print date('r');

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

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

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

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

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


Example   Finding time parts

            $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  prints:

            18:23:45
            18:23:45

Discussion

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.


Table   Return array from getdate()

Key         Value                                                                             
seconds   Seconds
minutes   Minutes
hours       Hours
mday       Day of the month
wday       Day of the week, numeric (Sunday is 0, Saturday is 6)
mon         Month, numeric
year         Year, numeric (4 digits)
yday        Day of the year, numeric (e.g., 299)
weekday  Day of the week, textual, full (e.g., “Friday”)
month      Month, textual, full (e.g., “January”)
0              Seconds since epoch (what time() returns)

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

Example   Finding the month, day, and year

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

Example   prints:

            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.


Example   getdate() with a specific timestamp

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

Example  prints:

            March 10, 1975

The function localtime() also returns an array of time and date parts. It also takes an epoch 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.


Table   Return array from localtime()

Numeric position  Key          Value                                            
0                            tm_sec     Second                                       
1                            tm_min    Minutes                                       
2                            tm_hour   Hour                                           
3                            tm_mday  Day of the month                       
4                            tm_mon    Month of the year (January is 0)
5                            tm_year    Years since 1900                       
6                            tm_wday  Day of the week (Sunday is 0)   
7                            tm_yday   Day of the year                           
8                            tm_isdst    Is daylight saving time in effect?


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

Example   Using localtime()

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

Example   prints:

            2/4/2013

The month is incremented by 1 before printing because localtime() starts counting months with 0 for January, but we want to display 1 if the current month is January. 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_default_timezone_set().



No comments:

Post a Comment

Post Top Ad