Share
Preview
Working with dates and times can be confusing. How does the Unix Timestamp work, exactly? And how do you handle factors such as time zone changes?
 ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌
ALEX WEB DEVELOP

Alex
The Unix Timestamp Explained
by Alex
Unix Timestamp


Hey,
Alex here.

To work with dates and times in PHP, you need to rely on the
Unix Timestamp.

So, what is this Unix Timestamp exactly, and why do you need it in the first place?
And how does it help with complications like time zone changes?


Let's take a closer look and see how the Unix Timestamp works.



What is the Unix Timestamp, exactly?

The Unix Timestamp is a number that increments by 1 every second (with one little exception that we'll see later).
It started counting on 1970-01-01, which is considered the birthday of the Unix operating system (so now you know where the name comes from!)

So, put simply, the Unix Timestamp tells how many seconds have passed since the beginning of the year 1970.

The reason why the Unix Timestamp is popular among programmers, is that it represents a single moment in time without ambiguities.

For instance, a datetime like "2022-01-14 13:24:43" is ambiguous, because you don't know in which time zone it is.
Also, daylight saving time rules can duplicate or skip hours, so the same hour can represent two different moments in time.

The Unix Timestamp has no such problems.


In PHP you can get the current Unix Timestamp by using time().
You can also convert any Unix Timestamp into a datetime representation by using date().
For example:

$unixTimestamp = time();
echo "The current Unix Timestamp is: $unixTimestamp  <br>";

$date = date('H:i:s d/m/Y', $unixTimestamp);
echo "And its datetime representation is: $date  <br>";



Now, you may ask: if the Unix Timestamp begins in 1970, how can you represent dates before that year?

Well, in PHP, the Unix Timestamp can go backwards and forward almost indefinitely. In particular, you can represent datetimes before 1970 by using negative numbers.

For example, you can calculate the Unix Timestamp of 1000 years before now simply by subtracting the number of seconds in 1000 years:

$now = time();
$past = $now - (1000 * 365 * 86400);
echo date('d/m/Y', $past);

Output:  18/09/1022




Time zone issues.

To see how handy the Unix Timestamp is, let's take an example.
Let's say that you have departure and arrival times for an international flight:

  • Departure:  10 Jan 2022, 11:30
  • Arrival:  10 Jan 2022, 14:30

But there's a problem: it's not clear in which time zone those times are.
The departure is probably in the airport time zone, but what about the arrival? Is it in the same time zone of the departure, or in the destination's?

To make things clear, you need to add the time zone information as well:

  • Departure:  10 Jan 2022, 11:30 (London)
  • Arrival:  10 Jan 2022, 14:30 (Tokyo)


Ok, that' better.
But what if the user wants to see both times in the departure timezone, or in the arrival's?
With the hour:minute+zone format, converting it between time zones is quite a challenge.

But if you use Unix Timestamps, it all becomes easy.

You can use the date_default_timezone_set() function to choose the timezone, and then use date() to represent the Unix Timestamp in that timezone:

$departureUT = 1641814200;
$arrivalUT = 1641857400;

date_default_timezone_set('Europe/London');
$departureLondon = date('d M Y, H:i', $departureUT) . ' (London)';
$arrivalLondon = date('d M Y, H:i', $arrivalUT) . ' (London)';

date_default_timezone_set('Asia/Tokyo');
$departureTokyo = date('d M Y, H:i', $departureUT) . ' ( Tokyo )';
$arrivalTokyo = date('d M Y, H:i', $arrivalUT) . ' ( Tokyo )';




The sneaky Leap Seconds.

Remember when we said that the Unix Timestamp increments every second, with just one exception?

For reasons explained in detail here, sometimes we need to "sync" the UTC time with the solar time.
When this happens, an extra second, called leap second, is added at the end of the year.

The Unix Timestamp is in sync with the UTC.
When a leap second is added,  the Unix Timestamp "skips" a second.
This is actually the only case where the Unix Timestamp is ambiguous. However, this happens so rarely that you can safely ignore it.

If you want to know more, including how to implement a high-precision solution, look here (beware: it's very technical).



Operations with mktime().

The mktime() function gives you the Unix Timestamp of a specific datetime.
It takes the date elements (year, month, day, hour, minute, second) as arguments.

The cool thing about mktime() is that you can perform intuitive operations with the date elements.
For instance, you can add or subtract days, hours or months, and the result will be what you expect.

For example, do you want to get the Unix Timestamp of exactly 200 days from now? Just add 200 to the current day, and mktime() will take care of everything.
(Use null to let mktime() use the current value for a date element):

$day = date('d');
$unixTimestamp = mktime(null, null, null, null, $day + 200);
echo date('d/m/Y', $unixTimestamp);




That's all for today.
Now send me a reply with your questions and let me know what you think. I would love to hear from you.

Until next time,
Alex



Share the knowledge

Did you like this email? Share it with your friends!
Click here to share it



Premium products to improve your skills

PHP courses

PHP Security Mastery
What if you could write code that is always secure from attacks?
Just imagine how that would increase your reputation and your possibilities as a developer.
That is exactly what you will get from this course.
You will learn to use the defense techniques that really work, leaving nothing to chance (with real code examples).

Take a look and see for yourself.


Resources

Alex Web Develop - My tutorials.
Alex PHP café
- My Facebook group where to talk with me and other developers.

Business vector created by pikisuperstar - 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