PHP Tutorial Tutorials - PHP Date and Time

PHP Date and Time

PHP Date/Time functions allow you to get the date and time from the server where your PHP scripts are running.
They are dependent on the locale settings of your server.

UNIX Timestamp is the time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).
For example: The corresponding Timestamp of January 1 1970 00:00:01 is 1.

The PHP Date() Function

The PHP date() function format a timestamp to a more readable date and time according to the given format string.

PHP date() function syntax:

date ( string $format [, int $timestamp = time() ] ) : string

$format specifies the format of returned date and time.
most common characters that are recognized in the format parameter string

format character Description Example
d Day, 2 digits with leading zeros 01 to 31
j Day without leading zeros 1 to 31
F A full textual representation of a month January through December
m Month, with leading zeros 01 through 12
n Month, without leading zeros 1 through 12
t Number of days in the given month 28 through 31
Y Year, 4 digits Examples: 1999 or 2017
y Year, 2 digits Examples: 99 or 17
h 12-hour format of an hour with leading zeros 01 through 12
H 24-hour format of an hour with leading zeros 00 through 23
i Minutes with leading zeros 00 to 59
s Seconds with leading zeros 00 through 59
<?php
    $today = date("d/m/Y");
    echo $today;
    echo "<br>";
    $now = date("Y-m-d H:i:s");
    echo $now;
?>

Outputs:

07/09/2017
2017-09-07 11:37:21

We can convert this timestamp to a human readable date/time using date() function:

<?php
    $timestamp = 1503571263;
    echo(date("F d, Y h:i:s", $timestamp));
?>

Outputs:

August 24, 2017 10:41:03

The PHP time() Function

The time() function is used to get the current Unix timestamp.

<?php
    $nowTimestamp = time();
    echo($nowTimestamp);
?>

Outputs:

1504784863

The PHP strtotime() Function

The strtotime() function is used to parse about any English textual datetime description into a Unix timestamp.

<?php
echo strtotime("now"), "\n";
echo strtotime("10 September 2000"), "\n";
echo strtotime("+1 day"), "\n";
echo strtotime("+1 week"), "\n";
echo strtotime('January 1 2017 00:00:00'), "\n";
echo strtotime('January 1 1970 00:03:00'), "\n";
?>

Outputs:

1567867337
968544000
1567953737
1568472137
1483228800
180

The PHP mktime() Function

The mktime() function is used to create the timestamp based on a specific date and time.

PHP mktime() function syntax:

mktime(hour, minute, second, month, day, year)
<?php
// Prints something like: 2006-04-05T01:02:03+00:00
echo date('c', mktime(1, 2, 3, 4, 5, 2006));

// Automatically calculate the correct value for out-of-range input
// Prints something like:Jan-01-1998
echo date("M-d-Y", mktime(0, 0, 0, 12, 32, 1997));
?>

Complete PHP Date Reference

Please Refer to the PHP Date/Time Functions section for a complete list of Date/time functions in PHP.

Date:2019-10-01 02:49:08 From:www.Lautturi.com author:Lautturi