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
IntlDateFormatterclass. Use this code.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.
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\IntlDateFormatterclasses.Example:
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.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().