简体   繁体   中英

PHP Date Format returning incorrect suffix ('S')

I have a Unix Timestamps such as 1423389830 and 1511367992 . I'm using a simple date() function: date( 'M nS, Y', $timestamp ); and it seems like it's returning the wrong date, with the appropriate suffix - which output some rather silly results.

Using the following sample code:

printf( '[%s] [%s]', date( 'M nS, Y', 1423389830 ), date( 'M nS, Y', 1511367992 ) );

On 3 servers: Mine, PHPTester.net , and OnlinePHPFucntions.com , the result is

[Feb 2th, 2015] [Nov 11nd, 2017]

Dropping those timestamps into EpochConverter.com I get and Sunday, February 8, 2015 2:03:50 AM GMT-08:00 and Wednesday, November 22, 2017 8:26:32 AM GMT-08:00 , respectively. February's date is 6 days early - and November's is 11 days early.

You can see an example here: http://sandbox.onlinephpfunctions.com/code/605415e0490af3e8e1dc3cf12ce9e1ec9ea2b3b6

So, as I wrote out this question, I realized that the day that was output was the same number as the month's number (Feb = 2nd month, November is the 11th month).

This lead me to look back at my date format and I realized I used the wrong format... the n should be a d or j : date( 'M jS, Y', $timestamp ); .

I figured I'll leave this for (rather embarrassing) posterity - and hopefully to help anyone who sees their PHP Date Suffix returning humorously incorrect to check their Date Format String and make sure you're actually using the appropriate/desired format

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