Help with Date Time Conversions
I'm trying to convert my date and time to a better format so I can compare it to a time and date 36 hours from now. Then I plan to compare these two so I can see which date is earlier.
My problem is that I can't just use the typical methods because the code is mixing my day and month.
Here's my code:
Here are my results:
$orig= $v_CurrentRecordD['day'].", ".$v_CurrentRecordD['time']; //MM/DD/YYYY, HH:MM
$conv = date('m/d/Y H:i', strtotime($orig));
$daycutoff = date('m/d/Y H:i', strtotime("+36 hours"));
echo $orig."-".$conv."-".$daycutoff." <br />";
06-20-2013, 8:00am-12/31/1969 18:00-06/05/2013 22:56
02-11-2013, 8:45am-11/02/2013 08:45-06/05/2013 22:56
02-11-2013, 8:15am-11/02/2013 08:15-06/05/2013 22:56
02-11-2013, 8:00am-11/02/2013 08:00-06/05/2013 22:56
06-01-2012, 10:15am-01/06/2012 10:15-06/05/2013 22:56
02-11-2013, 8:30am-11/02/2013 08:30-06/05/2013 22:56
02-12-2013, 10:45am-12/02/2013 10:45-06/05/2013 22:56
02-12-2013, 10:30am-12/02/2013 10:30-06/05/2013 22:56
I think the key here is to do your "date-time arithmetic" in the UNIX timestamp domain (which is an integer), not the formatted date string domain. So, just use the integer values returned by strtotime() (without processing them back into a string with date()) to do your comparisons, then just use date() in your output to make the results human-readable again.
Thanks, NogDog, but my problem is more the initial conversions than the arithmetic. The first two "columns" of my output are a little messed up because of the date and time format. The third "column" is today's date and time +36 hours, which seems to be working fine. (Sorry, I could've simplified it.)
Something is definitely messed up where I think it's mixing up days and months, and also there are some anomalies like in the first row.
Sounds like it might be a case where you might want to use 3 separate input fields for date, so that there is no confusion between the fact that, in general, 5/7/2013 is the same thing as 7-5-2013 for strtotime().
Thanks, but I can't change the variables.
...or can I? Is there a way to do that?
This string is the one I have to use, but if I can modify it into three variables, that would work.
Can that be done? How?
You can split that date string into the individual numbers using the explode function
$dateParts = explode("-",$v_CurrentRecordD['day']);
$month = $dateParts;
$day = $dateParts;
$year = $dateParts;
$timestamp = strtotime($year."-".$month."-".$day);
Last edited by iBeZi; 06-12-2013 at 12:19 PM.
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)