PHP & MySQL

PHP date_default_timezone_get(): Get default time zone UTC

When we are working with dates in PHP we always have to consider if the time zone of the system is correct. To obtain this information we have the function of PHP date_default_timezone_get() that will return a string with this zone.

Syntax of PHP date_default_timezone_get()

This is the syntax of the function extracted from the official website:

string date_default_timezone_get();

Example of PHP date_default_timezone_get()

Let’s see how the following code represents the time zone in the PHP example of PHP date_default_timezone_get().

<?php
    $timezone = date_default_timezone_get();
    echo 'The default time zone UTC is: ' . $timezone;
?>

Running this script on this server returns the following code:

The default time zone UTC is: Europe/Madrid

Time zones supported by PHP date_default_timezone_get()

The time zones that we have in PHP are those that appear in this link.

PHP code to return time difference

The following code is NOT mine, but it will be very useful, via php.net, you can see it working the same demo as before but in the example 2:

<?php
function timezone_offset_string( $offset )
{
        return sprintf( "%s%02d:%02d", ( $offset >= 0 ) ? '+' : '-', abs( $offset / 3600 ), abs( $offset % 3600 ) );
}

$offset = timezone_offset_get( new DateTimeZone( 'Europe/Berlin' ), new DateTime() );
echo "Time in Berlín: " . timezone_offset_string( $offset ) . "\n";

?>

PHP functions with dates

Share
Published by
Aner Barrena