Jump to content

time stamps


jpratt

Recommended Posts

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.