0

I ended up with the short end of the stick and a very verbose string as a date:

Sun Oct 5, 2014 10am to 11am  PDT

I would like to convert this to a PHP Date object so I can write it to a MySQL database. If you notice, it has a "to", which indicates event duration. I've searched for this conversion quite a bit and haven't found anything that isn't extensive string parsing. I'm open to all suggestions and ideas.

Jonny Sooter
  • 2,417
  • 1
  • 24
  • 40

2 Answers2

1

Cause this is not a valid time or date string you wont be able tp parse it as it is. You have to make some string parsing even if you seem to not like that way.

If you want to have a duration you got to make 2 dates and subtract the lower from the higher one

Marvin Fischer
  • 2,552
  • 3
  • 23
  • 34
0

Here is a work around for this. First need to parse the string and then generate the start and end date/time for the DB to save

$d = 'Sun Oct 5, 2014 10am to 11am  PDT';
$de = explode('to',$d);
$from = DateTime::createFromFormat('D M d, Y Ha', trim($de[0]));
$to_time = explode(' ',trim($de[1]));
$to =  DateTime::createFromFormat('Y-m-d Ha',$from->format("Y-m-d").' '.trim($to_time[0]));
echo $from->format("Y-m-d H:i:s");
echo '<br />';
echo $to->format("Y-m-d H:i:s");
echo '<br />';
Abhik Chakraborty
  • 44,654
  • 6
  • 52
  • 63