Share
Preview
Here are 6 interesting facts about working with datetimes in PHP that you should know.
 ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌
ALEX WEB DEVELOP

Alex
6 Things About Date and Time
That You Should Know
by Alex
Datetime


Hey,
Alex here.

Time is complicated.
Did you know that time passes a little bit faster when you are on the top of a building, compared to when you are down in the street?
That's because time flows differently depending on speed and gravity.

Fortunately, you can safely ignore all of that for the purpose of web programming.
Nonetheless, sometimes datetimes can be confusing.
So, here are 6 interesting facts about working with datetimes in PHP that you should know.



1. The Unix Timestamp.

When you need to represent a specific moment in time, the best solution is to use Unix Timestamps.
The Unix Timestamp is a numeric value that counts the seconds elapsed since the Unix epoch, which is 00:00:00 UTC on 1 January 1970.
You can print the current Unix Timestamp using the time() function:

echo time();

And you can get the Unix Timestamp of a specific date with the mktime() function.
For example, this is how to get the timestamp of August 25th 2010, 12:15:39 on your time zone:

mktime(12, 15, 39, 8, 25, 2010);

Handling datetimes with Unix Timestamps is very handy. You can easily represent them in different formats and perform operations on them.
Moreover, JavaScript supports Unix Timestamps too, so they are a good solution when creating dynamic JavaScript code as well.


2. The Time Zones.

When you work with datetimes, you must keep in mind that the time is different depending on the time zone you are in.
By default, PHP works on the same time zone as the server it runs on.
At any time, you can set the working time zone explicitly with the date_default_timezone_set() function.

A common time zone to use is UTC.
UTC is based on the GMT time zone (the Greenwich time zone, at Meridian 0), though it's not exactly the same.

UTC has no daylight saving time, so there are no duplicate or missing hours.
This is how to set the working time zone to UTC:

date_default_timezone_set('UTC');

Setting the correct time zone is particularly important when you need to:
  • Display a specific datetime.
  • Get the Unix Timestamp of a datetime with mktime().

Of course, the same moment in time is represented differently depending on which time zone you are in.

And for the same reason, when you use mktime(), the moment in time specified by the arguments (year, month, day, hour, minute and second) has a different meaning depending on the time zone you are in.


3. PHP Time Zone and MySQL Time Zone

Just like PHP considers the time zone when working with datetimes, so does the MySQL database.

By default, MySQL uses the time zone of the server it runs on (just like PHP).
So, datetime values returned by MySQL depend on the time zone. For example, if you run this query:

$query = 'SELECT NOW()';

The result will contain a representation of the current datetime.
This representation depends on the MySQL time zone, not on the PHP time zone.
Therefore, you need to be sure not to use different time zones between PHP and MySQL, to avoid getting wrong results.
For instance, if you set the PHP time zone to UTC, then you should also set the MySQL time zone to UTC to be sure that you are talking the same "time language".

A simple way to set the MySQL time zone is to run this query:

$query = 'SET timezone = "UTC"';


4. Leap Seconds

The Unix Timestamp increments by 1 for every second elapsed since the Unix Epoch.

However... this is not completely true.
In fact, some years contain an additional second on December 31st: the 23:50:60 second.
These seconds are called leap seconds.
When this happens, the Unix Timestamp "stays still" for one second.

This is a harmless event in most cases.

However, if you need high precision (for instance, to calculate the time difference between two events crossing the 23:50:60 time), you need to consider this fact.

If you want to learn more, you can look at my 3-parts PHP time handling guide here, here and here.


5. The mktime() Flexibility

I love how mktime() makes it easy to perform datetime operations.
As explained before, mktime() takes a datetime "coordinates" as arguments (hour, minute, second, day, month and year) and returns the corresponding Unix Timestamp.

A nice feature of mktime() is that you can add or subtract values from each of these arguments, and mktime() will take care of providing the correct result.
For instance, let's say that you have this base datetime: 10:15:01 February 15th 2000.
Now you want to get the Unix Timestamp of exactly 60 days after that.
You don't need to manually calculate the resulting month number and day number. You can simply add 60 to the days argument:

mktime(10, 15, 01, 2, 75, 2000);

The same goes for previous datetimes.
So, if you want to get the Unix Timestamp of 48 hours earlier, you can just subtract 48 to the hour argument:

mktime(-38, 15, 01, 2, 15, 2000);

mktime() will take care of handling all the math for you, including daylight saving hours if necessary.


6. Invalid and Ambigue times

One thing you should know about is the chance of representing non-existing or ambigue times.

This happens with daylight saving time rules, when hours can be skipped or repeated.
For example, here in Italy this datetime doesn't exist:
02:30:00 28 Match 2021

Because the time goes from 01:59:59 directly to 03:00:00.
And this can refer to two different moments in time:
02:30:00 31 October 2021

Because the time between 02:00:00 and 03:00:00 is repeated twice.
In fact, if you try getting the Unix Timestamp of this last date, PHP will return the Unix Timestamp of the second iteration.
This leads to fun results, such as the difference between 01:30:00 and 02:30:00 being 7200 seconds (2 hours)!

date_default_timezone_set('Europe/Rome');
$time1 = mktime(1, 30, 00, 10, 31, 2021);
$time2 = mktime(2, 30, 00, 10, 31, 2021);
echo ($time2 - $time1);


You can avoid such problems by working in UTC.
But if you prefer not to, be sure to handle such cases with care. MySQL databases too can cause query errors if you try using a non-existing datetime.



That's all for today.
Now it's your turn: send me a reply with your questions and thoughts.

Until next time (no pun intended),
Alex



Share the knowledge

Did you like this email? Share it with your friends and colleagues.
Click here to share it



You Don’t Want Your PHP Apps to be Hacked?
To make sure it will not happen, you need to know:
  • Which attacks you must prevent.
  • The specific defense techniques to stop each of those attacks.

PHP Security Mastery
is my security course that will teach you exactly how to do that.

Click here for the details.


Need help with your PHP code?
If you can't make your PHP code work, you can ask me for a Code Review.
I will verify your code, fix it and make sure it works the way you want.

Click here for the details.


Resources

Alex Web Develop - My blog where you can find my tutorials.
Alex PHP café
- My Facebook group where you can talk with me and other developers.

Business vector created by vectorjuice - www.freepik.com




You are receiving this newsletter because you subscribed to Alex Web Develop.

If you unsubscribe, you will not get any more emails from me.

Alessandro Castellano, P.IVA (VAT ID): 07012140484, via Luigi Morandi 32, 50141 Firenze FI, Italy

Email Marketing by ActiveCampaign