Dates

Updated on 28 Dec 2018

What day does Christmas fall on this year?

We can use the mktime with the date function to find all the information we’re looking for. First we use mktime to get the timestamp for Christmas for the current year. Then we pass that timestamp with the appropriate formatting flags to the date function to display the day.

$Christmas = mktime(0, 0, 0, 12, 25, date('Y'));
echo "\nChristmas will fall on a " . date('l', $Christmas) . ' this year';

Display Current date and Format it

echo "\n" . date('d-F-Y');
echo "\n" . date('jS F Y');

Additional notes

There are 31 format flags that can be used with the date function. Characters that are not recognized as format flags are printed as-is, as shown with the first example with the dashes.

The date function also takes an optional second parameter which is the timestamp in seconds. This is the UNIX timestamp, measured in seconds since January 1st 1970.

Insert a date into MySQL

There are 3 field types in MySQL that specificially deal with dates and times. They are DATE, DATETIME and TIMESTAMP. DATE only stores the date whereas DATETIME and TIMESTAMP also store a time as well. TIMESTAMP is referenced with UNIX epoch time and can only store dates from 1970 until 2037. If you require dates outside this range, then you should use DATETIME.

DATETIME store their values on the database table as 'YYYY-MM-DD HH:MM:SS'. DATE columns only store the date part, and TIMESTAMP generally stores data as 'YYYYMMDDHHMMSS', however this is dependent on the MySQL server that is running and the configuration settings.

Dates can be inserted with any of the following formats (the first two formats can be either a string or number).

YYYYMMDD
YYYYMMDDHHMMSS
'YYYY-MM-DD'
'YYYY-MM-DD HH:MM:SS'
INSERT INTO public_holidays values (0, 'Christmas Day', 20201225);
 
INSERT INTO public_holidays values (0, 'New Years', '2021-01-01 00:00:00');
 
INSERT INTO public_holidays values(0, 'EKKA', '2020-08-06');

Additional Notes

You can still supply a time value when inserting into a DATE column however only the date part will be saved in the database column.

Compare Dates

Consider these two dates (for the same year). 15th March and 10th April. There should be no argument that 10th of April occurs AFTER the 15th of March, but how do we write a comparison that asserts true? After all 15 is greater than 10 and M(arch) occurs after A(pril) on the alphabet.

Using the Ymd Format

If the date were formatted as Year Month Day in 4 digit year and 2 digit month and 2 digit day then we could make a direct comparison.

I.e 20120410 is greater than 20120315

This would be useful if we needed to see if today was not passed a certain date (an expiry date for instance).

$today = date('Ymd');
if($today > 20200410)
  echo 'Something has expired';

Using the TimeStamp

Another example is to convert both dates into UNIX timestamps which basically records the number of seconds that has elapsed since January 1st 1970. The conversion can be done with the mktime function.

$today = mktime();
$expire_date = mktime(0, 0, 0, date('m')  , date('d')+1, date('Y'));
 
if($today > $expire_date)
  echo 'Something has expired';

We could have used $today = date('U'); instead of $today = mktime(); if we wanted. They both return the same result.

Using DateTime Object

Since PHP 5.2, A DateTime object has been introduced to work with dates.

$early_date = DateTime::createFromFormat('Ymd', '20200315');
$later_date = DateTime::createFromFormat('d-F-Y', '10-April-2020');

$early_datestr = $early_date->format('Ymd');
$later_datestr = $later_date->format('Ymd');

if($early_datestr < $later_datestr) {
    echo "\nYay, early_date occurs before later_date";
}

Add a number of days to a date

Suppose you have a website where you need to set or display an expiry date 7 days from todays date.

Old school

The technique is to add 7 days as represented in seconds to todays date as represented in UNIX timestamp and then convert the result into a date format you want.

$today = date('U');
$days7 = 7 * 86400;
 
$end_date = $today + $days7;
$end_date = date('dS F Y', $end_date);
$today_date = date('dS F Y', $today);

echo "\nToday is $today_date and Library book is due back on the $end_date";

Additional Notes

86400 is the number of seconds in a day.

DateInterval

The DateInterval provides a new way of dealing with date intervals (i.e. adding up days or times).

$today = new DateTime();
$days7 = new DateInterval('P7D');

$due_date = clone($today);
$due_date->add($days7);

$today_datestr = $today->format('d-F-Y');
$due_datestr = $due_date->format('d-F-Y');

echo "\nUsing DateTime and DateInterval classes";
echo "\nToday is $today_datestr and Library book is due back on the $due_datestr";

Find the difference between dates

Lets look at an early example, and see if we can figure the number of days between these dates.

$early_date = DateTime::createFromFormat('Ymd', '20200315');
$later_date = DateTime::createFromFormat('d-F-Y', '10-April-2020');

$interval = $early_date->diff($later_date);

echo "\nNumber of Days " . $interval->format('%R%a');