A minimal script that returns date/time can be a good example for starting with PHP. But if you add other stuffs like format settings and requests handling, you will get an advanced time server to include in your workflow.
PHP date and time
PHP has several functions for date and time support, it has also many built-in predefined format settings (like RSS, ISO-8601, …) and time zones.
Check these links for more informations :
- https://www.php.net/manual/en/ref.datetime.php
- https://www.php.net/manual/en/datetime.format.php
- https://www.php.net/manual/en/class.datetimeinterface.php#datetime.constants.types
- https://www.php.net/manual/en/timezones.php
Basic usage
In PHP, you can get a formatted string of date and time easily by using any of these functions :
- “date()” : return the local time of the server, this can be adjusted by either changing the time zone in “php.ini” configuration file (if it is accessible in your hosting server) or by using the function “date-default-timezone-set()” that will sets the default timezone used by all date/time functions in the script.
- “gmdate()” : return the UTC time.
These functions accept two parameters : the formatting pattern, and an optional timestamp to be formatted (if left empty, the function will use the current time).
Let’s try this example, which will output a date like (2022/03/15 16:34:00 +00:00).
<?php
echo date(Y/m/d H:i:s P);
?>
Building the service
We can also trasnform that minimal script to a global date/time REST service, which will receive requests with parameters for changing the time zone and the result format, and will return an on-demand formatted and localized date/time string.
First, we will create a file named like “times.php”, and we will define variables for assigning each parameter’s value (“$format” for the format pattern and “$tz” for the time zone), like the following :
<?php
$format = ( $_REQUEST['format'] );
$tz = ( $_REQUEST['tz'] );
Next, we will implement a complex condition for applying the format pattern. This condition condition has 3 levels (nested conditions) that will the following :
- Check if “$format” is not empty, then it will go to second condition (nested), else it will use a format pattern that we choose like ($format = ‘Y/m/d H:i:s P’;).
- Check if “$format” value is the name of a constant (the name of a PHP predefined date/time format), by using the function “is_null()” (preceded with a “@” for ignoring warnings) that will check the returned value from the function “constant()” is not null, then it will use that constant value, else it will go to the third condition (nested).
- Check if “$format” value is equal to some string that we have set as names for our own predefined date/time format (for example here, we used short names to call the PHP predefined formats “ISO8061” and “RSS”).
When all these conditions failed, the format that will be passed to the final function is ‘Y/m/d H:i:s P’. As in the following code :
if ( $format != '' ) {
if ( ! @is_null(constant($format)) ) {
$format = constant($format);
} else {
if ( $format == 'iso8601' ) {
$format = DATE_ISO8601;
} else if ( $format == 'rss' ) {
$format = DATE_RSS;
}
}
} else {
$format = 'Y/m/d H:i:s P';
}
Last, we will implement a condition based on the applying of the “date_default_timezone_set()” function with the “$tz” value, then the script will output (“echo”) a localised date/time by using the function “date($format)”, else the script will output the UTC date/time (to avoid using the server local time) by using the function “gmdate($format)”. Like the following code :
if ( date_default_timezone_set($tz) ) {
echo date($format);
} else {
echo gmdate($format);
}
?>
The final script
In the end, you will get a script like this :
<?php
$format = ( $_REQUEST['format'] );
$tz = ( $_REQUEST['tz'] );
if ( $format != '' ) {
if ( ! @is_null(constant($format)) ) {
$format = constant($format);
} else {
if ( $format == 'iso8601' ) {
$format = DATE_ISO8601;
} else if ( $format == 'rss' ) {
$format = DATE_RSS;
}
}
} else {
$format = 'Y/m/d H:i:s P';
}
if ( date_default_timezone_set($tz) ) {
echo date($format);
} else {
echo gmdate($format);
}
?>
N. B.
You can also restrict the requests kind to be received, by changing the global variable for a specific parameter or in the whole script.
You can change “$_REQUEST” by either :
- “$_GET” (to receive only GET requests), or
- “$_POST” (to receive only POST requests).
Deploying and using the service
Now, you have to deploy the final script to a PHP hosting server (with a correct date/time). Just put the script file in the folder corresponding to its future address.
There are three methods for using it :
- Direct navigation in any web browser to the script URL and passing parameters as a GET request, such (https://yoursite/times.php?format=DATE_RSS).
- Calling the script from another script or web page, for example, in a widget.
- The PHP script can also act as a RESTful service, so you can send a request with parameters to this script and receive a response by using a REST client in a web or native app (desktop or mobile app).