Reputation: 1221
I'm attempting to do a strtotime on my datetime entries (never null) from a web service result set.
When looping and cnoverting, I'm finding that many are failing to convert with strtotime. What would the issue be? There's no difference in the formatting between the ones that work and ones that don't.
1431109502 - 08-05-2015 13:25:02
- 06-15-2015 12:05:32 -- Didn't work (any that start with - didn't convert)
- 06-25-2015 11:30:30
- 06-15-2015 10:58:56
- 06-27-2015 20:33:31
- 06-28-2015 00:13:37
1431054141 - 07-05-2015 22:02:21
- 07-17-2015 07:23:57
- 07-22-2015 02:30:24
1431090998 - 08-05-2015 08:16:38
1433774646 - 08-06-2015 09:44:06
EDIT: Here is some code I am using to attempt to get 100% conversions.
$t = date_create_from_Format( 'd-m-Y H:i:s', trim($arrCurrentAlarms[$i]["DateTime"]) );
$sqldt = $t->format('Y-m-d H:i:s');
Input given to me is always in this format: 06-25-2015 11:30:30 (month day year hr min sec)
Upvotes: 3
Views: 667
Reputation: 360672
That's because strtotime is guessing WRONG about your dates:
date('r', strtotime('08-05-2015 13:25:02')) -> Fri, 08 May 2015 13:25:02 -0500
date('r', strtotime('06-25-2015 11:30:30')) -> Wed, 31 Dec 1969 18:00:00 -0600
Note now the first value comes out as May 8th: strtotime is considering your dates to be dd-mm-yyyy
, when you really have mm-dd-yyyy
.
Note how the second date comes out as Dec 1969 - that's how integer 0
(aka boolean FALSE) comes out as a date in my particular time zone. FALSE for FAILURE, since there's no month 25
in the calendar.
Since it's guessing wrong, you'll have to do date_create_from_format()
instead, and TELL php what format your dates are in.
Upvotes: 3