1

I receive a date from an array in the below format:

2014-10-22 07:24:57 EDT

I want to convert this to echo only the month and day like this: Oct 22

My code below works fine when I'm changing from the 15-Feb-2009 format.

<?php

$date = DateTime::createFromFormat('D-M-Y', '15-Feb-2009');
$nice_date = $date->format('M j');

echo $nice_date;

?>

Here's what I have that doesn't work. Probably not even close.

$array_date = '2014-10-22 07:24:57 EDT';
$date = DateTime::createFromFormat('YY-MM-DD HH:II:SS EDT', $array_date);
$nice_date = $date->format('M j');

echo $nice_date;

I'm at a loss right now. Any ideas?

Kevin
  • 41,694
  • 12
  • 53
  • 70
Torads
  • 69
  • 1
  • 10
  • 1
    You can't just guess at the format letters. Read [the documentation](http://php.net/manual/en/datetime.createfromformat.php). The format `'2014-10-22 07:24:57 EDT'` corresponds to the PHP format string `'Y-m-d H:i:s e'`. – Mark Reed Oct 24 '14 at 04:05
  • This is a standard datetime format, no need to guess it, just use `new DateTime()`, like this [demo](https://eval.in/209704). – Glavić Oct 24 '14 at 06:16

4 Answers4

4

If you want to explicitly provide the format, you must provide the correct format.

$array_date = '2014-10-22 07:24:57 EDT';
$date = DateTime::createFromFormat('Y-m-d H:i:s e', $array_date);
$nice_date = $date->format('M j');
echo $nice_date;
Kevin
  • 41,694
  • 12
  • 53
  • 70
  • Thanks @ghost I guess i was relatively close considering my skill level. Love this site. – Torads Oct 24 '14 at 04:09
  • @Torads if you want more reference, consult on the [manual](http://php.net/manual/en/function.date.php), it has the mappings of all of meaning . glad this helped – Kevin Oct 24 '14 at 04:10
2
$str = "2014-10-22 07:24:57 EDT";

echo date("M d", strtotime($str)); // Oct 22
l'L'l
  • 44,951
  • 10
  • 95
  • 146
1

Try following ( i used date and strtotime ) :

$originalDate = "2014-10-22 07:24:57 EDT";
$newDate = date("M j", strtotime($originalDate));
echo $newDate;
turtle
  • 1,619
  • 1
  • 14
  • 30
-1

$array_date = '2014-10-22 07:24:57 EDT'; $date = DateTime::createFromFormat('YY-MM-DD HH:II:SS EDT', $array_date); $nice_date = date('M d',$date); echo $nice_date;

Len_D
  • 1,422
  • 1
  • 12
  • 21