PHP - tutorial - 06 -date, time

revision:


PHP - date and time

The PHP date() function formats a timestamp to a more readable date and time.

Syntax: date(format, timestamp)

parameters:

format: required; specifies the format of the timestamp.

timestamp: optional; specifies a timestamp. Default is the current date and time.

Characters that are commonly used for dates:

d - represents the day of the month (01 to 31);
m - represents a month (01 to 12);
Y - represents a year (in four digits);
l (lowercase 'L') - represents the day of the week.
Other characters - like "/", ".", or "-" - can also be inserted between the characters to add additional formatting.

examples:

Today is 2024/12/03
Today is 2024.12.03
Today is 2024-12-03
Today is Tuesday
© 2010-2024
code:
                <?php
                    echo "Today is " . date("Y/m/d") . "<br>";
                    echo "Today is " . date("Y.m.d") . "<br>";
                    echo "Today is " . date("Y-m-d") . "<br>";
                    echo "Today is " . date("l") . "<br>";
                ?>
                © 2010-<?php echo date("Y");?>
            

Characters that are commonly used for times:

H - 24-hour format of an hour (00 to 23);
h - 12-hour format of an hour with leading zeros (01 to 12);
i - minutes with leading zeros (00 to 59);
s - seconds with leading zeros (00 to 59);
a - lowercase "Ante meridiem" and "Post meridiem" (am or pm)

examples:

The time is 08:19:28am
code:
                <?php
                    echo "The time is " . date("h:i:sa");
                ?>
            

If the time you got back from the code is not correct, it's probably because your server is in another country or set up for a different timezone. If you need the time to be correct according to a specific location, you can set the timezone you want to use. E.g. date_default_timezone_set("America/New_York");

examples:

The time is 03:19:28am

The time is 04:19:28pm
code:
                <?php
                    date_default_timezone_set("America/New_York");
                    echo "The time is " . date("h:i:sa")."<br><br>";
                ?>
                <?php
                    date_default_timezone_set("Asia/Shanghai");
                    echo "The time is " . date("h:i:sa");
                ?>
            

The PHP mktime() function.

This function returns the Unix timestamp for a date. The Unix timestamp contains the number of seconds between the Unix Epoch (January 1 1970 00:00:00 GMT) and the time specified.

Syntax: mktime(hour, minute, second, month, day, year)

examples:

Created date is 2014-08-12 11:14:54am
code:
                <?php
                    $d=mktime(11, 14, 54, 8, 12, 2014);
                    echo "Created date is " . date("Y-m-d h:i:sa", $d);
                ?>
            

The PHP strtotime() function

This function is used to convert a human readable date string into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT).

Syntax: strtotime(time, now)

examples:

Created date is 2014-04-15 10:30:00pm

2024-12-04 12:00:00am

2024-12-07 12:00:00am

2025-03-03 04:19:28pm
code:
                <?php
                    $d=strtotime("10:30pm April 15 2014");
                    echo "Created date is " . date("Y-m-d h:i:sa", $d); 
                    echo "<br><br>";
                ?>
                <?php
                    $d=strtotime("tomorrow");
                    echo date("Y-m-d h:i:sa", $d) . "<br>";
                    echo "<br>";
                    $d=strtotime("next Saturday");
                    echo date("Y-m-d h:i:sa", $d) . "<br>";
                    echo "<br>";
                    $d=strtotime("+3 Months");
                    echo date("Y-m-d h:i:sa", $d) . "<br>";

                ?>
            

PHP - date/time formats

formats for date and time

d - The day of the month (from 01 to 31)

D - A textual representation of a day (three letters)

j - The day of the month without leading zeros (1 to 31)

l (lowercase 'L') - A full textual representation of a day

N - The ISO-8601 numeric representation of a day (1 for Monday, 7 for Sunday)

S - The English ordinal suffix for the day of the month (2 characters st, nd, rd or th. Works well with j)

w - A numeric representation of the day (0 for Sunday, 6 for Saturday)

F - A full textual representation of a month (January through December)

m - A numeric representation of a month (from 01 to 12)

M - A short textual representation of a month (three letters)

n - A numeric representation of a month, without leading zeros (1 to 12)

t - The number of days in the given month

L - Whether it's a leap year (1 if it is a leap year, 0 otherwise)

o - The ISO-8601 year number

Y - A four digit representation of a year

y - A two digit representation of a year

a - Lowercase am or pm

A - Uppercase AM or PM

B - Swatch Internet time (000 to 999)

g - 12-hour format of an hour (1 to 12)

G - 24-hour format of an hour (0 to 23)

h - 12-hour format of an hour (01 to 12)

H - 24-hour format of an hour (00 to 23)

i - Minutes with leading zeros (00 to 59)

s - Seconds, with leading zeros (00 to 59)

u - Microseconds (added in PHP 5.2.2)

e - The timezone identifier (Examples: UTC, GMT, Atlantic/Azores)

I (capital i) - Whether the date is in daylights savings time (1 if Daylight Savings Time, 0 otherwise)

O - Difference to Greenwich time (GMT) in hours (Example: +0100)

P - Difference to Greenwich time (GMT) in hours:minutes (added in PHP 5.1.3)

T - Timezone abbreviations (Examples: EST, MDT)

Z - Timezone offset in seconds. The offset for timezones west of UTC is negative (-43200 to 50400)

c - The ISO-8601 date (e.g. 2013-05-05T16:34:42+00:00)

r - The RFC 2822 formatted date (e.g. Fri, 12 Apr 2013 12:01:05 +0200)

U - The seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)

DateTimeZone class constant

1 = AFRICA

2 = AMERICA

4 = ANTARCTICA

8 = ARCTIC

16 = ASIA

32 = ATLANTIC

64 = AUSTRALIA

128 = EUROPE

256 = INDIAN

512 = PACIFIC

1024 = UTC

2047 = ALL

4095 = ALL_WITH_BC

4096 = PER_COUNTRY

is_assoc parameter - keys

[tm_sec] - seconds

[tm_min] - minutes

[tm_hour] - hour

[tm_mday] - day of the month

[tm_mon] - month of the year (January=0)

[tm_year] - Years since 1900

[tm_wday] - Day of the week (Sunday=0)

[tm_yday] - Day of the year

[tm_isdst] - Is daylight savings time in effect


PHP - date/time functions

The date/time functions allow you to get the date and time from the server where your PHP script runs. You can then use the date/time functions to format the date and time in several ways. These functions depend on the locale settings of your server. Remember to take daylight saving time and leap years into consideration when working with these functions.

checkdate(month, day, year) - validates a Gregorian date;

examples:

bool(false)
bool(false)
bool(true)
code:
                <?php
                    var_dump(checkdate(12,31,-400));
                    echo "<br>";
                    var_dump(checkdate(2,29,2003));
                    echo "<br>";
                    var_dump(checkdate(2,29,2004));
                ?>
            

date_add(object, interval) - adds days, months, years, hours, minutes, and seconds to a date.

examples:

2013-04-24
code:
                <?php
                    $date=date_create("2013-03-15");
                    date_add($date,date_interval_create_from_date_string("40 days"));
                    echo date_format($date,"Y-m-d");
                ?>
            

date_create_from_format(format, time, timezone) - returns a new DateTime object formatted according to a specified format;

examples:

2013/03/15
code:
                <?php
                    $date=date_create_from_format("j-M-Y","15-Mar-2013");
                    echo date_format($date,"Y/m/d");
                ?>
            

date_create(time, timezone) - returns a new DateTime object;

examples:

2013/03/15
2013/03/15 23:40+01:00
code:
                <?php
                $date=date_create("2013-03-15");
                echo date_format($date,"Y/m/d");
                echo "<br>";    
                $date=date_create("2013-03-15 23:40:00",timezone_open("Europe/Oslo"));
                echo date_format($date,"Y/m/d H:iP");
                ?>
            

date_date_set(object, year, month, day) - sets a new date;

examples:

2020/10/30
code:
                <?php
                    $date=date_create();
                    date_date_set($date,2020,10,30);
                    echo date_format($date,"Y/m/d");
                ?>
            

date_default_timezone_get() - returns the default timezone used by all date/time functions;

examples:

Asia/Shanghai
code:
                <?php
                    echo date_default_timezone_get();
                ?>
            

date_default_timezone_set(timezone) - sets the default timezone used by all date/time functions;

examples:

Asia/Bangkok
code:
                <?php
                    date_default_timezone_set("Asia/Bangkok");
                    echo date_default_timezone_get();
                ?>
            

date_diff(datetime1, datetime2, absolute) - returns the difference between two dates;

examples:

+272 days
code:
                <?php
                    $date1=date_create("2013-03-15");
                    $date2=date_create("2013-12-12");
                    $diff=date_diff($date1,$date2);
                    echo $diff->format("%R%a days");
                ?>
            

date_format(object, format) - returns a date formatted according to a specified format;

examples:

2013/03/15 00:00:00
code:
                <?php
                    $date=date_create("2013-03-15");
                    echo date_format($date,"Y/m/d H:i:s");  
                ?>
            

date_get_last_errors() - returns the warnings/errors found in a date string;

examples:

Array ( [warning_count] => 1 [warnings] => Array ( [6] => Double timezone specification ) [error_count] => 5 [errors] => Array ( [0] => The timezone could not be found in the database [10] => Unexpected character [11] => Unexpected character [12] => Unexpected character [13] => Unexpected character ) )
code:
                <?php
                    date_create("gyuiyiuyui%&&/");
                    print_r(date_get_last_errors());
                ?>
            

date_interval_create_from_date_string(datetime) - sets up a DateInterval from the relative parts of the string;

examples:

2020-02-05
code:
                <?php
                    $date = date_create('2019-01-01');
                    date_add($date, date_interval_create_from_date_string('1 year 35 days'));
                    echo date_format($date, 'Y-m-d');  
                ?>
            

date_interval_format(format) - formats the interval;

examples:

Total number of days: 40.
Total number of days: +40.
Month: 1, days: 9.
code:
                <?php
                    $date1=date_create("2013-01-01");
                    $date2=date_create("2013-02-10");
                    $diff=date_diff($date1,$date2);

                    // %a outputs the total number of days
                    echo $diff->format("Total number of days: %a.");
                    echo "<br>";

                    // %R outputs + beacause $date2 is after $date1 (a positive interval)
                    echo $diff->format("Total number of days: %R%a.");
                    echo "<br>";

                    // %d outputs the number of days that is not already covered by the month
                    echo $diff->format("Month: %m, days: %d.");
                ?>
            

date_isodate_set(object, year, week, day) - sets the ISO date;

examples:

2013-01-28
2013-01-29
code:
                <?php
                    $date=date_create();
                    date_isodate_set($date,2013,5);
                    echo date_format($date,"Y-m-d") . "<br>";
                    date_isodate_set($date,2013,5,2);
                    echo date_format($date,"Y-m-d");  
                ?>
            

date_modify(object, modify) - modifies the timestamp;

examples:

2013-05-16
code:
                <?php
                    $date=date_create("2013-05-01");
                    date_modify($date,"+15 days");
                    echo date_format($date,"Y-m-d");
                ?>
            

date_offset_get(object) - returns the timezone offset;

examples:

3600 seconds.
7200 seconds.
code:
                <?php
                    $winter=date_create("2013-12-31",timezone_open("Europe/Oslo"));
                    $summer=date_create("2013-06-30",timezone_open("Europe/Oslo"));

                    echo date_offset_get($winter) . " seconds.<br>";
                    echo date_offset_get($summer) . " seconds.";  
                ?>
            

date_parse_from_format(format, date) - returns an associative array with detailed info about a specified date, according to a specified format;

examples:

Array ( [year] => [month] => 12 [day] => 13 [hour] => [minute] => [second] => [fraction] => [warning_count] => 0 [warnings] => Array ( ) [error_count] => 1 [errors] => Array ( [8] => Not enough data available to satisfy format ) [is_localtime] => )

Array ( [year] => 2013 [month] => 5 [day] => 12 [hour] => 14 [minute] => 35 [second] => 0 [fraction] => 0 [warning_count] => 0 [warnings] => Array ( ) [error_count] => 0 [errors] => Array ( ) [is_localtime] => 1 [zone_type] => 1 [zone] => 7200 [is_dst] => )
code:
                <?php
                    print_r(date_parse_from_format("mmddyyyy","05122013"));
                    echo "<br><br>";
                    print_r(date_parse_from_format("j.n.Y H:iP","12.5.2013 14:35+02:00"));  
                ?>
            

date_parse(date) - returns an associative array with detailed info about a specified date;

examples:

Array ( [year] => 2013 [month] => 5 [day] => 1 [hour] => 12 [minute] => 30 [second] => 45 [fraction] => 0.5 [warning_count] => 0 [warnings] => Array ( ) [error_count] => 0 [errors] => Array ( ) [is_localtime] => )
code:
                <?php
                    print_r(date_parse("2013-05-01 12:30:45.5"));
                ?>
            

date_sub(object, interval) - subtracts days, months, years, hours, minutes, and seconds from a date;

examples:

2013-02-03
code:
                <?php
                    $date=date_create("2013-03-15");
                    date_sub($date,date_interval_create_from_date_string("40 days"));
                    echo date_format($date,"Y-m-d");
                ?>
            

date_sun_info(timestamp, latitude, longitude) - returns an array containing info about sunset/sunrise and twilight begin/end, for a specified day and location;

examples:

Date: 1st January, 2013

sunrise: 11:37:43
sunset: 21:47:39
transit: 16:42:41
civil_twilight_begin: 11:12:19
civil_twilight_end: 22:13:03
nautical_twilight_begin: 10:41:58
nautical_twilight_end: 22:43:24
astronomical_twilight_begin: 10:12:17
astronomical_twilight_end: 23:13:06

Date: 1st June, 2013

sunrise: 09:32:53
sunset: 23:40:56
transit: 16:36:54
civil_twilight_begin: 09:06:33
civil_twilight_end: 00:07:16
nautical_twilight_begin: 08:32:49
nautical_twilight_end: 00:41:00
astronomical_twilight_begin: 07:56:37
astronomical_twilight_end: 01:17:11
code:
                <?php
                    echo "<h4>Date: 1st January, 2013</h4>";
                    $sun_info=date_sun_info(strtotime("2013-01-01"),31.7667,35.2333);
                    foreach ($sun_info as $key=>$val) {
                        echo "$key: " . date("H:i:s",$val) . "<br>";
                    }
                    echo "<h4>Date: 1st June, 2013</h4>";
                    $sun_info=date_sun_info(strtotime("2013-06-01"),31.7667,35.2333);
                    foreach ($sun_info as $key=>$val){
                        echo "$key: " . date("H:i:s",$val) . "<br>";
                    }   
                ?>
            

date_sunrise(timestamp, format, latitude, longitude, zenith, gmtoffset) - returns the sunrise time for a specified day and location;

examples:

Lisbon, Portugal

Date: Tue Dec 03 2024
Sunrise time: 08:39
Sunset time: 18:12
code:
                <?php
                    //Calculate the sunrise time for Lisbon, Portugal
                    //Latitude: 38.4 North
                    //Longitude: 9 West
                    //Zenith ~= 90
                    //offset: +1 GMT

                    echo("<h4>Lisbon, Portugal</h4>");
                    echo("Date: " . date("D M d Y"));
                    echo("<br>Sunrise time: ");
                    echo(date_sunrise(time(),SUNFUNCS_RET_STRING,38.4,-9,90,1));
                    echo("<br>Sunset time: ");
                    echo(date_sunset(time(),SUNFUNCS_RET_STRING,38.4,-9,90,1));
                    
                ?>
            

date_sunset(timestamp, format, latitude, longitude, zenith, gmtoffset) - returns the sunset time for a specified day and location;

examples:

Lisbon, Portugal

Date: Tue Dec 03 2024
Sunrise time: 08:39
Sunset time: 18:12
code:
                <?php
                    //Calculate the sunrise time for Lisbon, Portugal
                    //Latitude: 38.4 North
                    //Longitude: 9 West
                    //Zenith ~= 90
                    //offset: +1 GMT

                    echo("<h4>Lisbon, Portugal</h4>");
                    echo("Date: " . date("D M d Y"));
                    echo("<br>Sunrise time: ");
                    echo(date_sunrise(time(),SUNFUNCS_RET_STRING,38.4,-9,90,1));
                    echo("<br>Sunset time: ");
                    echo(date_sunset(time(),SUNFUNCS_RET_STRING,38.4,-9,90,1));
                ?>
            

date_time_set(object, hour, minute, second, microseconds) - sets the time;

examples:

2013-05-01 13:24:00
2013-05-01 12:20:55
code:
                <?php
                    $date=date_create("2013-05-01");

                    date_time_set($date,13,24);
                    echo date_format($date,"Y-m-d H:i:s") . "<br>";

                    date_time_set($date,12,20,55);
                    echo date_format($date,"Y-m-d H:i:s");
                ?>
            

date_timestamp_get(object) - returns the Unix timestamp;

examples:

1733213968
code:
                <?php
                    $date=date_create();
                    echo date_timestamp_get($date);
                ?>
            

date_timestamp_set(object, unixtimestamp) - Sets the date and time based on a Unix timestamp;

examples:

1371803321 = 2013-06-21 15:28:41
code:
                <?php
                    $date=date_create();
                    date_timestamp_set($date,1371803321);
                    echo date_format($date,"U = Y-m-d H:i:s");
                ?>
            

date_timezone_get(object) - returns the time zone of the given DateTime object;

examples:

Europe/Paris
code:
                <?php
                    $date=date_create(null,timezone_open("Europe/Paris"));
                    $tz=date_timezone_get($date);
                    echo timezone_name_get($tz);
                ?>
            

date_timezone_set(object, timezone) - sets the time zone for the DateTime object;

examples:

2013-05-25 00:00:00+05:00
2013-05-24 21:00:00+02:00
code:
                <?php
                    $date=date_create("2013-05-25",timezone_open("Indian/Kerguelen"));
                    echo date_format($date,"Y-m-d H:i:sP") . "<br>";

                    date_timezone_set($date,timezone_open("Europe/Paris"));
                    echo date_format($date,"Y-m-d H:i:sP");
                ?>
            

date(format, timestamp) - formats a local date and time;

examples:

Tuesday
Tuesday 3rd of December 2024 03:19:28 PM
Oct 3,1975 was on a Friday
Tue, 03 Dec 24 15:19:28 +0700
1975-10-03T00:00:00+07:00
code:
                <?php
                    // Prints the day
                    echo date("l") . "<br>";

                    // Prints the day, date, month, year, time, AM or PM
                    echo date("l jS \of F Y h:i:s A") . "<br>";

                    // Prints October 3, 1975 was on a Friday
                    echo "Oct 3,1975 was on a ".date("l", mktime(0,0,0,10,3,1975)) . "<br>";

                    // Use a constant in the format parameter
                    echo date(DATE_RFC822) . "<br>";

                    // prints something like: 1975-10-03T00:00:00+00:00
                    echo date(DATE_ATOM,mktime(0,0,0,10,3,1975));
                ?>
            

getdate(timestamp) - returns date/time information of a timestamp or the current local date/time;

examples:

Array ( [seconds] => 28 [minutes] => 19 [hours] => 15 [mday] => 3 [wday] => 2 [mon] => 12 [year] => 2024 [yday] => 337 [weekday] => Tuesday [month] => December [0] => 1733213968 )

Tuesday, December 3, 2024
code:
                <?php
                    // Print the array from getdate()
                    print_r(getdate());
                    echo "<br><br>";

                    // Return date/time info of a timestamp; then format the output
                    $mydate=getdate(date("U"));
                    echo "$mydate[weekday], $mydate[month] $mydate[mday], $mydate[year]";
                ?>
            

gettimeofday(return_float) - returns the current time;

examples:

Array ( [sec] => 1733213968 [usec] => 675373 [minuteswest] => -420 [dsttime] => 0 )

1733213968.6754

1733213968.675375
code:
                <?php
                    // Print the array from gettimeofday()
                    print_r(gettimeofday());
                    echo "<br><br>";

                    // Print the float from gettimeofday()
                    echo gettimeofday(true) . "<br><br>";

                    // Return current time; then format the output
                    $mytime=gettimeofday();
                    echo "$mytime[sec].$mytime[usec]";
                ?>
            

gmdate(format, timestamp) - formats a GMT/UTC date and time;

examples:

Tuesday
Tuesday 3rd of December 2024 08:19:28 AM
Oct 3, 1975 was on a Thursday
Tue, 03 Dec 24 08:19:28 +0000
1975-10-02T17:00:00+00:00
code:
                <?php
                    // Prints the day
                    echo gmdate("l") . "<br>";

                    // Prints the day, date, month, year, time, AM or PM
                    echo gmdate("l jS \of F Y h:i:s A") . "<br>";
                    
                    // Prints October 3, 1975 was on a Thursday
                    echo "Oct 3, 1975 was on a ".gmdate("l", mktime(0,0,0,10,3,1975)) . "<br>";
                    
                    // Use a constant in the format parameter
                    echo gmdate(DATE_RFC822) . "<br>";
                    
                    // prints something like: 1975-10-03T00:00:00+00:00
                    echo gmdate(DATE_ATOM,mktime(0,0,0,10,3,1975));
                ?>
            

gmmktime(hour, minute, second, month, day, year, is_dst) - returns the Unix timestamp for a GMT date;

examples:

Oct 3, 1975 was on a Friday
code:
                <?php
                    // Prints: October 3, 1975 was on a Friday
                echo "Oct 3, 1975 was on a ".date("l", gmmktime(0,0,0,10,3,1975));
                ?>
            

gmstrftime(format, timestamp) - formats a GMT/UTC date and time according to locale settings;

examples:

December 31 1998, 13:00:00 GMT
2024. december 03. kedd. 08:19:28 GMT
code:
                <?php
                    echo(gmstrftime("%B %d %Y, %X %Z",mktime(20,0,0,12,31,98))."
"); setlocale(LC_ALL,"hu_HU.UTF8"); echo(gmstrftime("%Y. %B %d. %A. %X %Z")); ?>

idate(format, timestamp) - formats a local time/date as integer;

examples:

388
3
3
15
19
0
1
12
28
31
1733213968
2
49
24
2024
337
25200
code:
                <?php
                    echo idate("B") . "<br>";
                    echo idate("d") . "<br>";
                    echo idate("h") . "<br>";
                    echo idate("H") . "<br>";
                    echo idate("i") . "<br>";
                    echo idate("I") . "<br>";
                    echo idate("L") . "<br>";
                    echo idate("m") . "<br>";
                    echo idate("s") . "<br>";
                    echo idate("t") . "<br>";
                    echo idate("U") . "<br>";
                    echo idate("w") . "<br>";
                    echo idate("W") . "<br>";
                    echo idate("y") . "<br>";
                    echo idate("Y") . "<br>";
                    echo idate("z") . "<br>";
                    echo idate("Z") . "<br>";  
                ?>
            

localtime(timestamp, is_assoc) - returns the local time;

examples:

Array ( [0] => 28 [1] => 19 [2] => 15 [3] => 3 [4] => 11 [5] => 124 [6] => 2 [7] => 337 [8] => 0 )

Array ( [tm_sec] => 28 [tm_min] => 19 [tm_hour] => 15 [tm_mday] => 3 [tm_mon] => 11 [tm_year] => 124 [tm_wday] => 2 [tm_yday] => 337 [tm_isdst] => 0 )
code:
                <?php
                    print_r(localtime());
                    echo "<br><br>";
                    print_r(localtime(time(),true));
                ?>
            

microtime(return_float) - returns the current Unix timestamp with microseconds;

examples:

0.67998700 1733213968Did nothing in 0.0001978874206543 seconds
code:
                <?php
                    echo(microtime());
                    /**
                    * Simple function to replicate PHP 5 behaviour
                    */
                    function microtime_float()
                    {
                        list($usec, $sec) = explode(" ", microtime());
                        return ((float)$usec + (float)$sec);
                    }

                    $time_start = microtime_float();

                    // Sleep for a while
                    usleep(100);

                    $time_end = microtime_float();
                    $time = $time_end - $time_start;

                    echo "Did nothing in $time seconds\n";
                ?>
            

mktime(hour, minute, second, month, day, year, is_dst) - returns the Unix timestamp for a date;

examples:

Oct 3, 1975 was on a Friday

Jan-05-2002
Feb-01-2002
Jan-01-2001
Jan-01-1999
code:
                <?php
                    // Prints: October 3, 1975 was on a Friday
                    echo "Oct 3, 1975 was on a ".date("l", mktime(0,0,0,10,3,1975)) . "<br><br>";

                    //The mktime() function is useful for doing date arithmetic and validation.
                    //It will automatically calculate the correct value for out-of-range input:
                    echo date("M-d-Y",mktime(0,0,0,12,36,2001)) . "<br>";
                    echo date("M-d-Y",mktime(0,0,0,14,1,2001)) . "<br>";
                    echo date("M-d-Y",mktime(0,0,0,1,1,2001)) . "<br>";
                    echo date("M-d-Y",mktime(0,0,0,1,1,99)) . "<br>";
                ?>
            

strftime(format, timestamp) - formats a local time and/or date according to locale settings;

examples:

december 31 1998, 20:00:00 +07
2024. december 03. kedd. 15:19:28 +07
code:
                <?php
                    echo(strftime("%B %d %Y, %X %Z",mktime(20,0,0,12,31,98))."<br>");

                    setlocale(LC_ALL,"hu_HU.UTF8");
                    echo(strftime("%Y. %B %d. %A. %X %Z"));
                ?>
            

strptime(date, format) - parses a time/date generated with strftime();

Note: This function is not implemented on Windows platforms!

examples:

code:
                <?php
                    $format="%d/%m/%Y %H:%M:%S";
                    $strf=strftime($format);
                    echo("$strf");
                    print_r(strptime($strf,$format));      
                ?>
            

strtotime(time, now) - parses an English textual datetime into a Unix timestamp;

examples:

1733213968
1128272400
1733231968
1733818768
1734103173
1733677200
1732986000
code:
                <?php
                    echo(strtotime("now") . "<br>");
                    echo(strtotime("3 October 2005") . "<br>");
                    echo(strtotime("+5 hours") . "<br>");
                    echo(strtotime("+1 week") . "<br>");
                    echo(strtotime("+1 week 3 days 7 hours 5 seconds") . "<br>");
                    echo(strtotime("next Monday") . "<br>");
                    echo(strtotime("last Sunday"));  
                ?>
            

time() - returns the current time as a Unix timestamp;

examples:

1733213968
2024-12-03
code:
                <?php
                    $t=time();
                    echo($t . "<br>");
                    echo(date("Y-m-d",$t));  
                ?>
            

timezone_abbreviations_list() - returns an associative array containing dst, offset, and the timezone name;

examples:

Array ( [0] => Array ( [dst] => [offset] => 34200 [timezone_id] => Australia/Adelaide ) [1] => Array ( [dst] => [offset] => 34200 [timezone_id] => Australia/Broken_Hill ) [2] => Array ( [dst] => [offset] => 34200 [timezone_id] => Australia/Darwin ) [3] => Array ( [dst] => [offset] => 34200 [timezone_id] => Australia/North ) [4] => Array ( [dst] => [offset] => 34200 [timezone_id] => Australia/South ) [5] => Array ( [dst] => [offset] => 34200 [timezone_id] => Australia/Yancowinna ) )
code:
                <?php
                    $tzlist = DateTimeZone::listAbbreviations();
                    print_r($tzlist["acst"]);
                ?>
            

timezone_identifiers_list(what, country) - returns an indexed array with all timezone identifiers;

examples:

Array ( [0] => Africa/Abidjan [1] => Africa/Accra [2] => Africa/Addis_Ababa [3] => Africa/Algiers [4] => Africa/Asmara [5] => Africa/Bamako [6] => Africa/Bangui [7] => Africa/Banjul [8] => Africa/Bissau [9] => Africa/Blantyre [10] => Africa/Brazzaville [11] => Africa/Bujumbura [12] => Africa/Cairo [13] => Africa/Casablanca [14] => Africa/Ceuta [15] => Africa/Conakry [16] => Africa/Dakar [17] => Africa/Dar_es_Salaam [18] => Africa/Djibouti [19] => Africa/Douala [20] => Africa/El_Aaiun [21] => Africa/Freetown [22] => Africa/Gaborone [23] => Africa/Harare [24] => Africa/Johannesburg [25] => Africa/Juba [26] => Africa/Kampala [27] => Africa/Khartoum [28] => Africa/Kigali [29] => Africa/Kinshasa [30] => Africa/Lagos [31] => Africa/Libreville [32] => Africa/Lome [33] => Africa/Luanda [34] => Africa/Lubumbashi [35] => Africa/Lusaka [36] => Africa/Malabo [37] => Africa/Maputo [38] => Africa/Maseru [39] => Africa/Mbabane [40] => Africa/Mogadishu [41] => Africa/Monrovia [42] => Africa/Nairobi [43] => Africa/Ndjamena [44] => Africa/Niamey [45] => Africa/Nouakchott [46] => Africa/Ouagadougou [47] => Africa/Porto-Novo [48] => Africa/Sao_Tome [49] => Africa/Tripoli [50] => Africa/Tunis [51] => Africa/Windhoek )
code:
                <?php
                    // Print all timezones in Africa
                    print_r(timezone_identifiers_list(1));

                    // Print the whole list
                    //print_r(timezone_identifiers_list());
                ?>
            

timezone_location_get(object) - returns location information for a specified timezone;

examples:

Array ( [country_code] => TW [latitude] => 25.05 [longitude] => 121.5 [comments] => )
code:
                <?php
                    $tz=timezone_open("Asia/Taipei");
                    print_r(timezone_location_get($tz));
                ?>
            

timezone_name_from_ abbr(abbr, gmtoffset, isdst) - returns the timezone name from abbreviation;

examples:

America/New_York
Europe/Helsinki
code:
                <?php
                    echo timezone_name_from_abbr("EST") . "<br>";
                    echo timezone_name_from_abbr("",7200,0);
                ?>
            

timezone_name_get(object) - returns the name of the timezone;

examples:

Europe/Paris
code:
                <?php
                    $tz=timezone_open("Europe/Paris");
                    echo timezone_name_get($tz);
                ?>
            

timezone_offset_get(object, datetime) - returns the timezone offset from GMT (in seconds);

examples:

28800
code:
                <?php
                    $tz=timezone_open("Asia/Taipei");
                    $dateTimeOslo=date_create("now",timezone_open("Europe/Oslo"));
                    echo timezone_offset_get($tz,$dateTimeOslo);  
                ?>
            

timezone_open(timnezone) - creates new DateTimeZone object;

examples:

Europe/Paris
code:
                <?php
                    $tz=timezone_open("Europe/Paris");
                    echo timezone_name_get($tz);
                ?>
            

timezone_transitions_get(object, timestamp_start, timestamp_end) - returns all transitions for the timezone;

examples:

Array ( [ts] => -9223372036854775808 [time] => -292277022657-01-27T08:29:52+00:00 [offset] => 561 [isdst] => [abbr] => LMT )

Array ( [ts] => -9223372036854775808 [time] => -292277022657-01-27T08:29:52+00:00 [offset] => 561 [isdst] => [abbr] => LMT )
code:
                <?php
                    $timezone = new DateTimeZone("Europe/Paris");
                    // Procedural style
                    print_r(reset(timezone_transitions_get($timezone)));

                    echo "<br><br>";

                    // Object oriented style
                    print_r(reset($timezone->getTransitions()));
                ?>
            

timezone_version_get() - returns the version of the timezonedb;

examples:

0.system
code:
                <?php
                    echo timezone_version_get();
                ?>
            

PHP - predefined date/time constants

DATE_ATOM - Atom (example: 2019-01-18T14:13:03+00:00);
DATE_COOKIE - HTTP Cookies (example: Fri, 18 Jan 2019 14:13:03 UTC);
DATE_ISO8601 - ISO-8601 (example: 2019-01-18T14:13:03+0000);
DATE_RFC822 - RFC 822 (example: Fri, 18 Jan 2019 14:13:03 +0000);
DATE_RFC850 - RFC 850 (example: Friday, 18-Jan-19 14:13:03 UTC);
DATE_RFC1036 - RFC 1036 (example: Friday, 18-Jan-19 14:13:03 +0000);
DATE_RFC1123 - RFC 1123 (example: Fri, 18 Jan 2019 14:13:03 +0000);
DATE_RFC2822 - RFC 2822 (example: Fri, 18 Jan 2019 14:13:03 +0000);
DATE_RFC3339 - Same as DATE_ATOM (since PHP 5.1.3);
DATE_RFC3339_EXTENDED - RFC3339 Extended format (since PHP 7.0.0) (example: 2019-01-18T16:34:01.000+00:00);
DATE_RSS - RSS (Fri, 18 Jan 2019 14:13:03 +0000);
DATE_W3C - World Wide Web Consortium (example: 2019-01-18T14:13:03+00:00);
SUNFUNCS_RET_TIMESTAMP - Timestamp (since PHP 5.1.2);
SUNFUNCS_RET_STRING - Hours:minutes (example: 09:41) (since PHP 5.1.2);
SUNFUNCS_RET_DOUBLE - Hours as a floating point number (example: 9.75) (since PHP 5.1.2).