Viewed   90 times

When I pull the date out of the db, it comes back like this:

2009-10-14T19:00:00

I want to format it in two different ways...

The first: F d, Y The second h:m (12 hour format)

Everything I try returns December 1969... Help?! I feel so confused...

 Answers

1

Normally the code is just:

echo date('F d, Y h:mA', strtotime('2009-10-14 19:00:00'));

Note that if strtotime() can't figure out the date, it returns the time as 1/1/1970 00:00:00 GMT.

Monday, December 19, 2022
4

Use createFromFormat method first, provide the input format:

$exd = DateTime::createFromFormat('d M, Y', '01 Dec, 2015');
// arguments (<format of the input>, <the input itself>)
$exd = date_format($exd, 'Y-m-d'); // then choose whatever format you like
echo $exd;
Thursday, September 8, 2022
 
2

Use DateTime:

$date = '2012-09-09 03:09:00';

$createDate = new DateTime($date);

$strip = $createDate->format('Y-m-d');
var_dump($strip); // string(10) "2012-09-09"

$now = new DateTime();
$difference = $now->diff($createDate, true);
var_dump($difference);

/* object(DateInterval)#3 (8) {
  ["y"]=>
  int(0)
  ["m"]=>
  int(0)
  ["d"]=>
  int(7)
  ["h"]=>
  int(13)
  ["i"]=>
  int(4)
  ["s"]=>
  int(38)
  ["invert"]=>
  int(0)
  ["days"]=>
  int(7)
} */
Monday, August 22, 2022
5

http://userguide.icu-project.org/datetime/timezone#TOC-Factory-Methods-and-the-Default-Tim says

TimeZone maintains a static time zone object known as the default time zone. This is the time zone that is used implicitly when the user does not specify one. ICU attempts to match this to the host OS time zone.

In short, if you want to change the default timezone from intl to match what date() says, you must change the time zone on your operating system. But don't do that.

It is preferred that you specify the timezone in the call to IntlDateFormatter::create(). If you wish to use the default timezone that PHP is using elsewhere, that can be retrieved with date_default_timezone_get().

$dateFormater = IntlDateFormatter::create(
    'en_EN',
    IntlDateFormatter::LONG,
    IntlDateFormatter::NONE,
    date_default_timezone_get()
);
Saturday, December 24, 2022
3

I ended up formatting the code in the controller action instead.

I just cast the datetime property to a string using .ToString() and got the desired results.

Thanks for the help though guys.

Friday, August 12, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :