jpratt Posted April 23, 2008 Share Posted April 23, 2008 I am storing date/time formatted like this: 04-23-2008 12:00 I have the following code to output a string as a date. My ultimate goal it to find differences between dates and split them based on appointments made. I have the following code: $timestr = "04-23-2008 11:00"; echo date('m-d-Y h:i', strtotime($timestr)); My only problem is it returns 12-31-1969 05:00, not the date/time given it. How do I convert the string to a timestamp to work with and get the correct date/time? Thanks. Link to comment https://forums.phpfreaks.com/topic/102553-time-stamps/ Share on other sites More sharing options...
craygo Posted April 23, 2008 Share Posted April 23, 2008 if you are going to use dates in a database you should be storing the values as a date, datetime, or timestamp, not a string. That being said, the strtotime function doesn't do well when you use dashes with the year being last. It works fine with 2008-04-23 but not the way you have it. try this instead $timestr = str_replace("-", "/", "04-23-2008 11:00"); echo date('m-d-Y h:i', strtotime($timestr)); Ray Link to comment https://forums.phpfreaks.com/topic/102553-time-stamps/#findComment-525112 Share on other sites More sharing options...
jpratt Posted April 23, 2008 Author Share Posted April 23, 2008 Thanks, works great! I did not know strtotime had this problem. Im working on a function that splits the times based on a selected time. The time will ultimately be stored as datetime in a mysql db. Thanks for the help. Link to comment https://forums.phpfreaks.com/topic/102553-time-stamps/#findComment-525118 Share on other sites More sharing options...
PFMaBiSmAd Posted April 23, 2008 Share Posted April 23, 2008 strtotime does not understand mm-dd-yyyy. If you had used slashes mm/dd/yyyy it would have worked. If these are stored in a database, you should be using a standard DATETIME data type: yyyy-mm-dd HH:MM:SS Doing so will allow you to directly sort, compare, and perform date calculations using about 20-30 mysql date and time functions. You can output a standard DATETIME data type using the mysql DATE_FORMAT() function in any format you want. With a mm-dd-yyyy format, you cannot directly do any of these things and the code necessary will result in slow operations over using a standard mysql format. Link to comment https://forums.phpfreaks.com/topic/102553-time-stamps/#findComment-525120 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.