Asked  7 Months ago    Answers:  5   Viewed   38 times

Facebook outputs dates in ISO8601 format - e.g.: 2011-09-02T18:00:00

Using PHP, how can I reformat into something like: Friday, September 2nd 2011 at 6:00pm

Nb - I was doing it in Javascript, but IE has date bugs so I want a cross-browser solution.

 Answers

33

A fast but sometimes-unreliable solution:

$date = '2011-09-02T18:00:00';

$time = strtotime($date);

$fixed = date('l, F jS Y at g:ia', $time); // 'a' and 't' are escaped as they are a format char.

Format characters detailed here.

Wednesday, March 31, 2021
 
samayo
answered 7 Months ago
48

ffmpeg is a great library for this sort of thing. Here's a walkthrough of the process:

http://vexxhost.com/blog/2007/05/20/how-to-convertencode-files-to-flv-using-ffmpeg-php/

Wednesday, March 31, 2021
 
Noob_Programmer
answered 7 Months ago
72

DateTime::createFromFormat and date_parse_from_format have been added in PHP 5.3 because there was a high demand for that feature, especially from developpers who code for users who don't use US date/time formats.


Before those, you had to develop a specific function to parse the format you were using ; with PHP < 5.3, what is generally done is :

  • Decide which format will be accepted by the application
  • Display some message saying something like "your input should be JJ/MM/AAAA" (French for DD/MM/YYYY)
  • Check that the input is OK, regarding to that format
  • And parse it to convert it to a date/time that PHP can understand.

Which means applications and developpers generally didn't allow for that many formats, as each format meant one different additionnal validation+parsing function.


If you really need that kind of function, that allows for any possible format, I'm afraid you'll kind of have to write it yourself :-(

Maybe taking a look at the sources of date_parse_from_format could help, if you understand C code ? It should be in something like ext/date/php_date.c -- but doesn't seem to be that simple : it's calling the timelib_parse_from_format function, which is defined in ext/data/lib/parse_date.c, and doesn't look that friendly ^^

Friday, May 28, 2021
 
Palladium
answered 5 Months ago
41

Use the pandas datetools parser to parse the date and then format it using the standard python strftime function.

>>> df['q_date'].apply(
        lambda x: pd.datetools.parse(x).strftime('%Y-%m-%dT%H:%M%:%SZ'))
0    20120210T00:0000Z
1    20120210T00:0000Z
2    20120210T00:0000Z
3    20120210T00:0000Z
4    20120210T00:0000Z
Name: q_date, dtype: object
Thursday, August 12, 2021
 
Blacksonic
answered 3 Months ago
51

The first parameter to date_format needs to be an object of DateTime class.

echo "<td>" . date_format( new DateTime($row['date']), 'd/m/Y H:i:s' ). "</td>";

or, alternatively

echo "<td>" . date_format( date_create($row['date']), 'd/m/Y H:i:s' ). "</td>";
Monday, August 16, 2021
 
julesj
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :