Home > OS >  Php IntlDateFormatter class does not show unix timestamp
Php IntlDateFormatter class does not show unix timestamp

Time:02-12

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.

CodePudding user response:

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 use \Datetime() class either.

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!

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().

  • Related