Time at it's simplest is just a series of increasing seconds.
However, keeping track of time across the world is a much more difficult practice. Let’s look at the basics of how time works and then we will see ways how we manage it in computers.
Understanding UTC
Coordinated Universal Time or UTC is the primary time standard by which the world regulates clocks and time. UTC, or Coordinated Universal Time, isn’t an actual time zone, but it is a time standard. It doesn’t change for Daylight Saving, and all other time zones are measured in their relation to UTC.
Time zones around the world are expressed using positive or negative offsets from UTC which has a range of UTC-12 to UTC+14.
India and Shri lanka uses Indian standard time ( IST ) which is offset UTC+5:30 i am 5 hours 30 minutes ahead of UTC.
How Computers record time
In computing, an epoch is a fixed date and time used as a reference from which a computer measures system time.
it’s an arbitrary date with no real meaning.
For Unix systems it is - 01 jan 1970 00:00:00 UTC
For Windows it is - 01 jan 1601 00:00:00 UTC
The Unix time 0 is exactly midnight UTC on 1 January 1970, with Unix time incrementing by 1 for every second after this.
Each day in this kind of system is about 24 x 60 x 60 = 86,400 seconds long.
Today when i am writing this article it is 19 Nov 2023 3:53 PM and the unix time is 1700389351 seconds .
So for computers time is nothing but a incremented integer value .
For precise time measurement computers uses other systems like PTSS designed by engineers synced with some atomic clock.
So a question comes in mind how do we read those big integer values to human readbale format . Well that's where programming languages come for our help we programmers format these number's to human readable format so that we do not have to count how many seconds have passed . it's overall an abstraction to complex tech used to measure time.
On Unix-like systems, the computer keeps its clock in UTC and applies an offset based on your time zone. like if UTC time is 1,700,389,351 then for indian public software would have to add 5.5 x 60 x 60 = 19800 to UTC time to get Indian standard time.
In Windows, the system clock is stored as your local time i.e for Indian public it is already UTC+5:30 .
Largest date in computer
For 32 bit Operating system
largest integer value is 2^32 - 1 which is 4,294,967,295 .
So largest date possible is 19 Jan 2038 , after that value will overflow and we have to recaliberate it for 32bit OS.
For 64 bit Operating system
largest integer value is 2^64 - 1 which is 18,446,744,073,709,551,615 .
So largest date possible is 21 July 2554 , which is really really far away so currently we don't have to worry about 64bit OS.
Current unix time | 32 bit OS largest integer | 64 bit OS largest integer |
2^32 - 1 | 2^64 - 1 | |
1,700,389,351 | 4,294,967,295 | 18,446,744,073,709,551,615 |
19 Nov 2023 | 19 Jan 2038 | 21 July 2554 |
Network time protocol ( NTP )
Network Time Protocol (NTP) is an internet protocol used to synchronize with computer clock time sources in a network.
How does NTP work?
The NTP client initiates a time-request exchange with the NTP server.
The client is then able to calculate the link delay and its local offset and adjust its local clock to match the clock at the server's computer.
As a rule, six exchanges over a period of about five to 10 minutes are required to initially set the clock.
Once synchronized, the client updates the clock about once every 10 minutes, usually requiring only a single message exchange, in addition to client-server synchronization. This transaction occurs via User Datagram Protocol (UDP) on port 123.
There are thousands of NTP servers around the world. They have access to highly precise atomic clocks and Global Positioning System clocks.
What are stratum levels?
Degrees of separation from the UTC source are defined as strata. The various strata include the following :
Stratum 0. A reference clock receives true time from a dedicated transmitter or satellite navigation system. It is categorized as stratum 0.
Stratum 1. A device is directly linked to the reference clock.
Stratum 2. A device receives its time from a stratum 1 computer.
Stratum 3. A device receives its time from a stratum 2 computer.
Working with time in PHP
So after a long time learning about time we finally know that computers store dates and times as timestamps because it is easier to manipulate an integer.
For example, to add one day to a timestamp, it simply adds the number of seconds to the timestamp.
PHP provides some helpful functions that manipulate timestamps effectively.
Getting the current time
<?php
echo time(); // 1700389351
The return value is a big integer that represents the number of seconds since Epoch.
To make the time human-readable, you use the date() function. For example:
<?php
$current_time = time();
echo date('Y-m-d g:ia', $current_time); // 2023-11-19 5:47am
The date() function has two parameters.
The first parameter specifies the date and time format.
The second parameter is an integer that specifies the timestamp.
Since the time() function returns a timestamp, we can add seconds to it.
Adding and subtracting from timestamp
The following example shows how to add a week to the current time:
<?php
$current_time = time();
// 7 days later
$one_week_later = $current_time + 7 * 24 * 60 * 60;
echo date('Y-m-d g:ia',$one_week_later);
Timezone
By default, the time()
function returns the current time in the timezone specified in the PHP configuration file (php.ini).
To get the current timezone, you can use the date_default_timezone_get()
function:
<?php
echo(date_default_timezone_get());
To set a specific timezone, you use the date_default_timezone_set()
. It’s recommended that you use the UTC timezone.
The following shows how to use the date_default_timezone_set()
function to set the current timezone to the UTC timezone:
<?php
date_default_timezone_set('UTC');
Making a unix timestamp
To make a Unix timestamp, we use the mktime()
function :
mktime(
int $hour,
int|null $minute = null,
int|null $second = null,
int|null $month = null,
int|null $day = null,
int|null $year = null
): int|false
The mktime()
function returns a Unix timestamp based on its arguments. If you omit an argument, mktime()
function will use the current value according to the local date and time instead.
The following example shows how to use the mktime()
function to show that Nov 19, 2023, is on a Sunday :
<?php
echo 'Nov 19, 2023 is on a ' . date('l', mktime(0, 0, 0, 11, 19, 2023));
Conclusion
Remember , time at it's simplest is just a series of increasing seconds for a computer geek.