New Page  |  Edit Page

 

Date & Time

A timestamp in PHP is a numeric value in seconds between the current time and value as at 1st January, 1970 00:00:00 Greenwich Mean Time (GMT).


$_SERVER['REQUEST_TIME'];

$_SERVER['REQUEST_TIME_FLOAT']; 

time()

The Unix epoch (or Unix time or POSIX time or Unix timestamp) is the number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT), not counting leap seconds (in ISO 8601: 1970-01-01T00:00:00Z). Literally speaking the epoch is Unix time 0 (midnight 1/1/1970), but 'epoch' is often used as a synonym for Unix time. Some systems store epoch dates as a signed 32-bit integer, which might cause problems on January 19, 2038 (known as the Year 2038 problem or Y2038).


time();

strtotime("15 November 2018");

date()

PHP date function is an in-built function that simplify working with date data types.


date(output format, epoch);

The PHP date function is used to format a date or time into a human readable format. It can be used to display the date of article was published. record the last updated a data in a database.


$today = date("F j, Y, g:i a");                 // March 10, 2001, 5:16 pm
$today = date("m.d.y");                         // 03.10.01
$today = date("j, n, Y");                       // 10, 3, 2001
$today = date("Ymd");                           // 20010310
$today = date('h-i-s, j-m-y, it is w Day');     // 05-16-18, 10-03-01, 1631 1618 6 Satpm01
$today = date('\i\t \i\s \t\h\e jS \d\a\y.');   // it is the 10th day.
$today = date("D M j G:i:s T Y");               // Sat Mar 10 17:16:18 MST 2001
$today = date('H:m:s \m \i\s\ \m\o\n\t\h');     // 17:03:18 m is month
$today = date("H:i:s");                         // 17:16:18
$today = date("Y-m-d H:i:s");                   // 2001-03-10 17:16:18 (the MySQL DATETIME format)

Timezone


$timezone = date_default_timezone_get();
echo "The current server timezone is: " . $timezone;

date_default_timezone_set('Australia/Melbourne');
date_default_timezone_set('America/Los_Angeles');

DateTime()


use \Datetime;

$now = new DateTime();
echo $now->format('Y-m-d H:i:s');    // MySQL datetime format
echo $now->getTimestamp();           // Unix Timestamp -- Since PHP 5.3

Timezone


$now = new DateTime(null, new DateTimeZone('America/New_York'));
$now->setTimezone(new DateTimeZone('Europe/London'));    // Another way
echo $now->getTimezone();
Before we look at how to set the default time zone programmatically, let’s look at how to get a list of supported time zones.

$timezone_identifiers = DateTimeZone::listIdentifiers();
foreach($timezone_identifiers as $key => $list){
    echo $list . "";
}

mktime()


//mktime(hour, minute, second, month, day, year, is_dst);
echo mktime(0,0,0,10,13,2025);

Parameters

Time
r Returns the full date and time
a, A Returns whether the current time is am or pm, AM or PM respectively
g, G Returns the hour without leading zeroes [1 to 12], [0 to 23] respectively
h, H Returns the hour with leading zeros [01 to 12],[00 to 23] respectively
i, s Returns the minutes/seconds with leading zeroes [00 to 59]
Day
d Returns the day of the month with leading zeroes [01 to 31]
j Returns the day of the month without leading zeroes [1 to 31]
D Returns the first 3 letters of the day name [Sub to Sat]
l Returns day name of the week [Sunday to Saturday]
w Returns day of the week without leading zeroes [0 to 6] Sunday is represent by zero (0) through to Saturday represented by six (6)
z Returns the day of the year without leading spaces [0 through to 365]
Month
n Returns the month number without leading zeroes [01 to 12]
M Returns the first 3 letters of the month name [Jan to Dec]
F Returns the month name [January to December]
t Returns the number of days in a month [28 to 31]
Year
L Returns 1 if it’s a leap year and 0 if it is not a leap year
Y Returns four digit year format
y Returns two (2) digits year format (00 to 99)