4

I have an XML file with a "Time" attribute. I am calling this XML into my PHP and giving it the variable $game_time.

An example value for the attribute is "10:30 PM"...

Since it is coming from XML, it is reading "10:30 PM" as a string.

Now my question, how can I convert this string to an actual timestamp?

I think I need to use strtotime somehow, but can't get the syntax right...

I am wanting this XML time to be converted to a timestamp because I am eventually going to compare this time with the server's time and shoot out a conditional output...

Thanks, John

John Stamos
  • 103
  • 6
  • No love from anyone? No clue? No help? – John Stamos Aug 24 '11 at 17:33
  • 2
    You will need to be a bit more patient, not every question on here is answered within a minute. – Gazler Aug 24 '11 at 17:34
  • you mean `strtotime($game_time)` is not giving you the correct output? – Ibu Aug 24 '11 at 17:35
  • When $game_time is set to "8:30 PM" strtotime($game_time) is outputting 1314232200... Now how do I convert that output to a timestamp that looks like "10:30" again... Basically, I am trying to convert a string (which is actually a time), to a data type (time) that would allow me to compare it with the server's time. – John Stamos Aug 24 '11 at 17:39

2 Answers2

7

you can use:

date_default_timezone_set('America/Los_Angeles');

$xmldata = '10:30 PM';
echo strtotime($xmldata);
echo date('Y-m-d H:i:s', strtotime($xmldata));

that will output:

1314250200

2011-08-24 22:30:00

If you don't specify a DATE, the current day will be use.

Book Of Zeus
  • 49,509
  • 18
  • 174
  • 171
  • Now how do I convert that output to a timestamp that looks like "10:30" again... Basically, I am trying to convert a string (which is actually a time), to a data type (time) that would allow me to compare it with the server's time. – John Stamos Aug 24 '11 at 17:42
  • as a reminder, if you use php 5.1.0 or before, strtotime will return -1 otherwise it returns FALSE. so you can always add validation in your script to make sure you have a valid time. – Book Of Zeus Aug 24 '11 at 17:53
4

you can use strtotime

<?
date_default_timezone_set('UTC');
$time_stamp = strtotime("10:00 AM"); //1314180000
echo date("F j, Y, g:i a", $time_stamp); //August 24, 2011, 10:00 am
?>
dvdmn
  • 6,456
  • 7
  • 44
  • 52