0

I'm working with a string that represents a timestamp in this format:

Mon Jul 13 11:32:00 EST 2015

I now need to convert this into a timestamp to insert into my database in this format:

MM/DD/YYYY HH:MM:SS

I would usually use this:

date("m/d/Y h:i:s A", $timestamp)

but this isn't working. I don't need to worry about timezones as the timestamp is in the local timezone already

user982124
  • 4,416
  • 16
  • 65
  • 140

2 Answers2

0

You need to convert the timestamp string into a time first. If you want to ignore the timezone, you can do something like:

// Set the time
$time = "Mon Jul 13 11:32:00 EST 2015";
// Cut out the timezone details
$time = str_replace(" EST", "", $time);
// Convert the string time into a time and then into a timestamp
$timestamp = date("m/d/Y h:i:s A", strtotime($time));
// echo $timestamp = "07/13/2015 11:32:00 AM"

Or, you can use something like mktime() (http://php.net/mktime) but this requires you splitting up your time string into separate elements.

mongjong
  • 479
  • 3
  • 6
0

Just two lines of code. Using Datetime object will make your code cleaner.

<?php
$datetime = new Datetime("Mon Jul 13 11:32:00 EST 2015");
print $datetime->format("m/d/Y h:i:s A");
rotvulpix
  • 468
  • 3
  • 9