简体   繁体   中英

PHP Datetime->format returning wrong ordinal suffix

I have a UTC time string that is not playing nice with PHP's DateTime->format function.

I create a new php file and paste in the following:

<?php
$date = DateTime::createFromFormat('U', 1567209600, new DateTimeZone('Australia/Brisbane') );

die( $date->format( 'l, nS F Y' ) );

I am getting:

Saturday, 8st August 2019

For starters, the correct date is 31, and the ordinal suffix for 8 should be 'th'. What is actually going on here? There is no other code in this file.

Note the codes are case sensitive. N is ordinal day of the week and n is the ordinal month. You want j , which is day of the month without leading zeroes: l, jS FY

$date = DateTime::createFromFormat('U', 1567209600, new DateTimeZone('Australia/Brisbane') );

var_dump($date);

The time zone is not set correctly.

Output

object(DateTime)#2 (3) { ["date"]=> string(26) "2019-08-31 00:00:00.000000" ["timezone_type"]=> int(1) ["timezone"]=> string(6) "+00:00" }

If the date was created from a timestamp, the time zone is always UTC. You must use setTimeZone after create to transfer to your time zone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM