I'm trying to get the unix timestamp with IntlDateFormatter.
I've tried this
$formatter = new IntlDateFormatter(
'fr_FR',
IntlDateFormatter::FULL,
IntlDateFormatter::FULL,
'Europe/Paris',
IntlDateFormatter::TRADITIONAL,
'unix'
);
echo $formatter->format(strtotime('now'));
Gave me 2022, while I'm trying to get it in this format 1644601950.
I've also tried to change unix with U AND u etc. but I can't find the right keyword for the unix timestamp in IntlDateFormatter class.
If I change the 'unix' to 'YY-MM-d' it would give me 22-02-11, but it's not in unix timestamp format.
Quick answer
To get unix timestamp with IntlDateFormatter class. Use this code.
$formatter = new IntlDateFormatter(
'fr_FR',
IntlDateFormatter::FULL,
IntlDateFormatter::FULL,
'Europe/Paris',
IntlDateFormatter::TRADITIONAL
);
echo $formatter->parse($formatter->format(strtotime('now')));
It seems that the only way to get timestamp from this class is only use IntlDateFormatter::parse() method.
Timestamp
Timestamp is always measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT) whatever functions or classes you use to get it. Example time(), mktime(), DateTime::getTimestamp().
Get timestamp from specific date/time.
In this example, assume that my server php.ini timezone is Asia/Bangkok and you get this date/time in Asia/Macau.
// server php.ini timezone is Asia/Bangkok
// set the date/time input.
// assume that it is Asia/Macau timezone.
$datetime = '2025-03-12 15:34:26';
$timestamp = strtotime($datetime);
echo $timestamp.'<br>';// 1741768466
// if you use function `date()`, the result will be wrong! because the timestamp is incorrectly parse using server timezone.
echo 'use date(): ' . date('Y-m-d H:i:s P', $timestamp) . '<br>';// 2025-03-12 15:34:26 +07:00 WRONG! incorrect timezone.
// even if you use `\IntlDateFormatter()` class, with or without set timezome the result still be wrong!
$formatter = new \IntlDateFormatter(
'en',
\IntlDateFormatter::FULL,
\IntlDateFormatter::FULL,
new \DateTimeZone('Asia/Macau'),
\IntlDateFormatter::GREGORIAN,
'yyyy-MM-dd kk:mm:ss ZZZZZ'
);
echo $formatter->format($timestamp).'<br>';// 2025-03-12 16:34:26 +08:00 WRONG! time does not matched.
$formatter->setTimeZone(new \DateTimeZone('Asia/Macau'));
echo $formatter->format($timestamp).'<br>';// 2025-03-12 16:34:26 +08:00 WRONG! time does not matched.
All the example above is wrong because the timestamp is parsed base on server timezone (php.ini).
Get time stamp in correct timezone.
As #Álvaro González commented, you can use strtotime('date/time timzone'); to get timstamp in certain timzone but you can also use \Datetime() or \IntlDateFormatter classes.
Example:
// server php.ini timezone is Asia/Bangkok
// set the date/time input.
// assume that it is Asia/Macau timezone.
$datetime = '2025-03-12 15:34:26';
$timestamp = strtotime($datetime . ' Asia/Macau');
echo $timestamp .'<br>';// 1741764866
echo 'use date(): ' . date('Y-m-d H:i:s P', $timestamp) . '<br>';// 2025-03-12 14:34:26 +07:00 CORRECT! but time zone is Bangkok as specified in php.ini (-1 hour for Macau to Bangkok).
$formatter = new \IntlDateFormatter(
'en',
\IntlDateFormatter::FULL,
\IntlDateFormatter::FULL,
new \DateTimeZone('Asia/Macau'),
\IntlDateFormatter::GREGORIAN,
'yyyy-MM-dd kk:mm:ss ZZZZZ'
);
echo $formatter->format($timestamp).'<br>';// 2025-03-12 15:34:26 +08:00 CORRECT!
// use `DateTime()` class to get timestamp in certain timezone.
$date = new DateTime($datetime, new \DateTimeZone('Asia/Macau'));
$timestamp2 = (int) $date->getTimestamp();
echo $timestamp2.'<br>';// 1741764866
$formatter = new \IntlDateFormatter(
'en',
\IntlDateFormatter::FULL,
\IntlDateFormatter::FULL,
new \DateTimeZone('Asia/Macau'),
\IntlDateFormatter::GREGORIAN,
'yyyy-MM-dd kk:mm:ss ZZZZZ'
);
echo $formatter->format($timestamp2) . '<br>';// 2025-03-12 15:34:26 +08:00 CORRECT!
// use `\IntlDateFormatter()` class to get timestamp in certain timezone.
$formatter = new IntlDateFormatter(
'en-US',
IntlDateFormatter::NONE,
IntlDateFormatter::NONE,
new \DateTimeZone('Asia/Macau'),
IntlDateFormatter::GREGORIAN,
'yyyy-MM-dd kk:mm:ss'
);
$timestamp3 = (int) $formatter->parse($datetime);
echo $timestamp3 . '<br>';// 1741764866 CORRECT!
To get timestamp from \IntlDateFormatter() class, you have to set the pattern in constructor matched the date/time source format.
Convert date/time across timezone
In this example I'll convert date/time result across timezone based on date/time that have got from Asia/Macau timezone.
I will use IntlDateFormatter() class to convert.
// server php.ini timezone is Asia/Bangkok
// set the date/time input.
// assume that it is date/time from Asia/Macau timezone.
$datetime = '2025-03-12 15:34:26';
$tsmacau = strtotime($datetime . ' Asia/Macau');// 1741764866
$formatter = new \IntlDateFormatter(
'en',
\IntlDateFormatter::FULL,
\IntlDateFormatter::FULL,
new \DateTimeZone('Asia/Macau'),// timezone for input timestamp.
\IntlDateFormatter::GREGORIAN,
'yyyy-MM-dd kk:mm:ss ZZZZZ'
);
// convert to Asia/Bangkok timezone.
$formatter->setTimezone(new \DateTimeZone('Asia/Bangkok'));
echo $formatter->format($tsmacau).'<br>';// 2025-03-12 14:34:26 +07:00
// convert to Europe/Paris timezone
$formatter->setTimezone(new \DateTimeZone('Europe/Paris'));
echo $formatter->format($tsmacau).'<br>';// 2025-03-12 08:34:26 +01:00
In this case you must know the timezone of timestamp because you have to set the input timezone into class constructor and change the timezone you want to convert into before call to format().
I want to convert date UTC for Europe/Lisbon, but the code I have gives me different outputs/times:
$datafull = "13-04-2021 08:47:13";
$date = new DateTime($datafull);
$date->setTimezone(new DateTimeZone('Europe/Lisbon'));
echo $date->format('d-m-Y H:i:s (e)');
// 13-04-2021 09:47:13 (Europe/Lisbon)
$datetime = new DateTime($datafull, new DateTimeZone('Europe/Lisbon'));
print $datetime->format('d-m-Y H:i:s (e)');
// 13-04-2021 08:47:13 (Europe/Lisbon)
When you supply a timezone object to the DateTime constructor you're telling it in what timezone the give $datafull is. So in:
$datetime = new DateTime($datafull, new DateTimeZone('Europe/Lisbon'));
You say it is in Europe/Lisbon, and it stays there.
In the other code:
$date = new DateTime($datafull);
$date->setTimezone(new DateTimeZone('Europe/Lisbon'));
The default timezone is used when the DateTime is constructed, probably UTC on your server, and then you change it afterwards on the second line to Europe/Lisbon, which is an hour ahead.
See: DateTime::__construct
I have a datetime value in this format 2019-07-01T05:21:08.148986Z which is in UTC.
How can I convert this timestamp into local time in human readable format?
FROM A DATETIME OBJECT:
You just need to modify the timezone :
$date->setTimezone(new \DateTimeZone(date_default_timezone_get() ) );
echo $date->format('d/m/Y H:i:s') . "\n";
You can also set a Time Zone beforehand :
date_default_timezone_set('Europe/Paris');
FROM A STRING:
Convert your date in timestamp :
$timestamp = strtotime($yourDate);
Then create a date using the timezone you want :
$date = new DateTime();
$date->setTimestamp($timestamp);
$date->setTimezone(new \DateTimeZone(date_default_timezone_get()));
echo $date->format('d/m/Y H:i:s') . "\n";
You can also set a Time Zone beforehand :
date_default_timezone_set('Europe/Paris');
I have a question. I try to use datetime in php.
I did :
$now = new \DateTime();
When I print_r the $now I have :
DateTime Object
(
[date] => 2016-12-01 05:55:01
[timezone_type] => 3
[timezone] => Europe/Helsinki
)
When I look at clock I have 16:05. I need to set the timezone ? I want to use Bucharest timezone. How I can get the right date and hour ? Thx in advance
You have two ways to set right timezone. It is object way and procedural way.
Examples
Object
$datetime = new DateTime();
$timezone = new DateTimeZone('Europe/Bucharest');
$datetime->setTimezone($timezone);
echo $datetime->format('F d, Y H:i');
Procedural
date_default_timezone_set("Europe/Bucharest");
$date = date('F d, Y H:i');
echo $date;
Manuals
PHP: date
PHP: DateTime
PHP: DateTimeZone
Update
Check code below, may it will work for you:
<?php
date_default_timezone_set('Europe/London');
$datetime = new DateTime();
$timezone = new DateTimeZone('Europe/Bucharest');
$datetime->setTimezone($timezone);
echo $datetime->format('F d, Y H:i');
?>
There are examples in the manual, you can set the timezone on the instantiation of the DateTime class like this
$now = new \DateTime('now', new DateTimeZone('Europe/Bucharest'));
put this line of code above your script:
date_default_timezone_set('Europe/Bucharest');
<?php
$datetime = new DateTime( "now", new DateTimeZone( "Europe/Bucharest" ) );
echo $datetime->format( 'Y-m-d H:i:s' );
Demo repl.it
You can use setTimezone() method of DateTime class to set the timezone to Europe/Bucharest, like this:
$now = new \DateTime();
$now->setTimezone(new DateTimeZone('Europe/Bucharest'));
Here's the reference:
http://php.net/manual/en/datetime.settimezone.php
I am in need of an easy way to convert a date time stamp to UTC (from whatever timezone the server is in) HOPEFULLY without using any libraries.
Use strtotime to generate a timestamp from the given string (interpreted as local time) and use gmdate to get it as a formatted UTC date back.
Example
As requested, here’s a simple example:
echo gmdate('d.m.Y H:i', strtotime('2012-06-28 23:55'));
Using DateTime:
$given = new DateTime("2014-12-12 14:18:00");
echo $given->format("Y-m-d H:i:s e") . "\n"; // 2014-12-12 14:18:00 Asia/Bangkok
$given->setTimezone(new DateTimeZone("UTC"));
echo $given->format("Y-m-d H:i:s e") . "\n"; // 2014-12-12 07:18:00 UTC
Try the getTimezone and setTimezone, see the example
(But this does use a Class)
UPDATE:
Without any classes you could try something like this:
$the_date = strtotime("2010-01-19 00:00:00");
echo(date_default_timezone_get() . "<br />");
echo(date("Y-d-mTG:i:sz",$the_date) . "<br />");
echo(date_default_timezone_set("UTC") . "<br />");
echo(date("Y-d-mTG:i:sz", $the_date) . "<br />");
NOTE: You might need to set the timezone back to the original as well
Do this way:
gmdate('Y-m-d H:i:s', $timestamp)
or simply
gmdate('Y-m-d H:i:s')
to get "NOW" in UTC.
Check the reference:
http://www.php.net/manual/en/function.gmdate.php
If you have a date in this format YYYY-MM-HH dd:mm:ss,
you can actually trick php by adding a UTC at the end of your "datetime string" and use strtotime to convert it.
date_default_timezone_set('Europe/Stockholm');
print date('Y-m-d H:i:s',strtotime("2009-01-01 12:00"." UTC"))."\n";
print date('Y-m-d H:i:s',strtotime("2009-06-01 12:00"." UTC"))."\n";
This will print this:
2009-01-01 13:00:00
2009-06-01 14:00:00
And as you can see it takes care of the daylight savings time problem as well.
A little strange way to solve it.... :)
Convert local time zone string to UTC string.
e.g. New Zealand Time Zone
$datetime = "2016-02-01 00:00:01";
$given = new DateTime($datetime, new DateTimeZone("Pacific/Auckland"));
$given->setTimezone(new DateTimeZone("UTC"));
$output = $given->format("Y-m-d H:i:s");
echo ($output);
NZDT: UTC+13:00
if $datetime = "2016-02-01 00:00:01", $output = "2016-01-31 11:00:01";
if $datetime = "2016-02-29 23:59:59", $output = "2016-02-29 10:59:59";
NZST: UTC+12:00
if $datetime = "2016-05-01 00:00:01", $output = "2016-04-30 12:00:01";
if $datetime = "2016-05-31 23:59:59", $output = "2016-05-31 11:59:59";
https://en.wikipedia.org/wiki/Time_in_New_Zealand
If you don't mind using PHP's DateTime class, which has been available since PHP 5.2.0, then there are several scenarios that might fit your situation:
If you have a $givenDt DateTime object that you want to convert to UTC then this will convert it to UTC:
$givenDt->setTimezone(new DateTimeZone('UTC'));
If you need the original $givenDt later, you might alternatively want to clone the given DateTime object before conversion of the cloned object:
$utcDt = clone $givenDt;
$utcDt->setTimezone(new DateTimeZone('UTC'));
If you only have a datetime string, e.g. $givenStr = '2018-12-17 10:47:12', then you first create a datetime object, and then convert it. Note this assumes that $givenStr is in PHP's configured timezone.
$utcDt = (new DateTime($givenStr))->setTimezone(new DateTimeZone('UTC'));
If the given datetime string is in some timezone different from the one in your PHP configuration, then create the datetime object by supplying the correct timezone (see the list of timezones PHP supports). In this example we assume the local timezone in Amsterdam:
$givenDt = new DateTime($givenStr, new DateTimeZone('Europe/Amsterdam'));
$givenDt->setTimezone(new DateTimeZone('UTC'));
As strtotime requires specific input format, DateTime::createFromFormat could be used (php 5.3+ is required)
// set timezone to user timezone
date_default_timezone_set($str_user_timezone);
// create date object using any given format
$date = DateTime::createFromFormat($str_user_dateformat, $str_user_datetime);
// convert given datetime to safe format for strtotime
$str_user_datetime = $date->format('Y-m-d H:i:s');
// convert to UTC
$str_UTC_datetime = gmdate($str_server_dateformat, strtotime($str_user_datetime));
// return timezone to server default
date_default_timezone_set($str_server_timezone);
I sometime use this method:
// It is not importnat what timezone your system is set to.
// Get the UTC offset in seconds:
$offset = date("Z");
// Then subtract if from your original timestamp:
$utc_time = date("Y-m-d H:i:s", strtotime($original_time." -".$offset." Seconds"));
Works all MOST of the time.
http://php.net/manual/en/function.strtotime.php or if you need to not use a string but time components instead, then http://us.php.net/manual/en/function.mktime.php
With PHP 5 or superior, you may use datetime::format function (see documentation http://us.php.net/manual/en/datetime.format.php)
echo strftime( '%e %B %Y' ,
date_create_from_format('Y-d-m G:i:s', '2012-04-05 11:55:21')->format('U')
); // 4 May 2012
try
echo date('F d Y', strtotime('2010-01-19 00:00:00'));
will output:
January 19 2010
you should change format time to see other output
General purpose normalisation function to format any timestamp from any timezone to other.
Very useful for storing datetimestamps of users from different timezones in a relational database. For database comparisons store timestamp as UTC and use with gmdate('Y-m-d H:i:s')
/**
* Convert Datetime from any given olsonzone to other.
* #return datetime in user specified format
*/
function datetimeconv($datetime, $from, $to)
{
try {
if ($from['localeFormat'] != 'Y-m-d H:i:s') {
$datetime = DateTime::createFromFormat($from['localeFormat'], $datetime)->format('Y-m-d H:i:s');
}
$datetime = new DateTime($datetime, new DateTimeZone($from['olsonZone']));
$datetime->setTimeZone(new DateTimeZone($to['olsonZone']));
return $datetime->format($to['localeFormat']);
} catch (\Exception $e) {
return null;
}
}
Usage:
$from = ['localeFormat' => "d/m/Y H:i A", 'olsonZone' => 'Asia/Calcutta'];
$to = ['localeFormat' => "Y-m-d H:i:s", 'olsonZone' => 'UTC'];
datetimeconv("14/05/1986 10:45 PM", $from, $to); // returns "1986-05-14 17:15:00"
As an improvement on Phill Pafford's answer (I did not understand his 'Y-d-mTG:i:sz' and he suggested to revert timezone).
So I propose this (I complicated by changing the HMTL format in plain/text...):
<?php
header('content-type: text/plain;');
$my_timestamp = strtotime("2010-01-19 00:00:00");
// stores timezone
$my_timezone = date_default_timezone_get();
echo date(DATE_ATOM, $my_timestamp)."\t ($my_timezone date)\n";
// changes timezone
date_default_timezone_set("UTC");
echo date("Y-m-d\TH:i:s\Z", $my_timestamp)."\t\t (ISO8601 UTC date)\n";
echo date("Y-m-d H:i:s", $my_timestamp)."\t\t (your UTC date)\n";
// reverts change
date_default_timezone_set($my_timezone);
echo date(DATE_ATOM, $my_timestamp)."\t ($my_timezone date is back)\n";
?>
alternatively you can try this:
<?php echo (new DateTime("now", new DateTimeZone('Asia/Singapore')))->format("Y-m-d H:i:s e"); ?>
this will output :
2017-10-25 17:13:20 Asia/Singapore
you can use this inside the value attribute of a text input box if you only want to display a read-only date.
remove the 'e' if you do not wish to show your region/country.
Follow these steps to get UTC time of any timezone set in user's local system (This will be required for web applications to save different timezones to UTC):
Javascript (client-side):
var dateVar = new Date();
var offset = dateVar.getTimezoneOffset();
//getTimezoneOffset - returns the timezone difference between UTC and Local Time
document.cookie = "offset="+offset;
Php (server-side):
public function convert_utc_time($date)
{
$time_difference = isset($_COOKIE['offset'])?$_COOKIE['offset']:'';
if($time_difference != ''){
$time = strtotime($date);
$time = $time + ($time_difference*60); //minutes * 60 seconds
$date = date("Y-m-d H:i:s", $time);
} //on failure of js, default timezone is set as UTC below
return $date;
}
..
..
//in my function
$timezone = 'UTC';
$date = $this->convert_utc_time($post_date); //$post_date('Y-m-d H:i:s')
echo strtotime($date. ' '. $timezone)