2

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.

dvlpr.963
  • 384
  • 1
  • 4
  • 15
  • Refer from their document, the `IntlDateFormatter` class [constructor](https://www.php.net/manual/en/intldateformatter.create.php) use 6th arguments as pattern. And they point out to this website ( https://unicode-org.github.io/icu/userguide/format_parse/datetime/ ) which has no `unix` pattern. – vee Feb 11 '22 at 18:18
  • I understand this is probably a simplified test case to illustrate the case, but `strtotime()` is literally the Unix time you're looking for. – Álvaro González Feb 11 '22 at 18:22
  • If you really have to use `IntlDateFormatter` class to get timestamp, use [`parse()`](https://www.php.net/manual/en/intldateformatter.parse.php) method. Remove 6th argument on constructor and use `$formatter->parse($formatter->format(time()));` – vee Feb 11 '22 at 18:27
  • @ÁlvaroGonzález Thanks for trying to help me, that's right `strtotime` returns the unix timestamp, but let's say I'm in China/Japan and receive a date-time in the future, I'm trying to format that Chinese/Japanese date-time in unix timestamp. – dvlpr.963 Feb 11 '22 at 18:27
  • @vee Thanks for trying to help me, I'll try your suggestion and let you know if it works. – dvlpr.963 Feb 11 '22 at 18:30

1 Answers1

3

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

vee
  • 4,506
  • 5
  • 44
  • 81
  • In the real code, `$formatter->format(strtotime('now'))` can be replaced with the string that contains the localised date. BTW, Unix time does not really belong to any time zone because it isn't a local time but an absolute moment in time. – Álvaro González Feb 11 '22 at 19:13
  • @ÁlvaroGonzález Thank you, I was refer timezone is GMT from time() function document page itself but currently I'm stuck with timezone conversion that is not return as expected. Maybe I'll delete this answer. – vee Feb 11 '22 at 19:17
  • `strtotime()` works with the default time zone. A better example could be `strtotime('now Europe/Paris')`, or just replace everything with a full date in French like `vendredi 11 février 2022 à 20:19:56 heure normale d’Europe centrale`. – Álvaro González Feb 11 '22 at 19:20
  • @ÁlvaroGonzález About your input, `strtotime('now Europe/Paris')`, is there any way that I could get a difference between a date time in the future and the current French native date time, `strtotime('now Europe/Paris')`? – dvlpr.963 Feb 11 '22 at 19:45
  • @dvlpr.963 I've updated the answer by add the ways to get timestamp from timezone use server timezone or specific timezone. I hope that it is useful for you. – vee Feb 11 '22 at 19:59
  • @vee Yes it's definitely very useful, thank you! I'm trying yo convert `vendredi 11 février 2022 à 20:19:56 heure normale d’Europe centrale` to its equivalent in English UTC. I'm so confused at the moment, there are too many date time functions and methods. – dvlpr.963 Feb 11 '22 at 20:11
  • I've just added example code for convert the result into across timezones. – vee Feb 11 '22 at 20:19