PHP strtotime conversion not working for some time

I am trying to make strtotime in my datetime records (never null) from a web service result set.

When looping and cnoverting, I find that many cannot convert from strtotime. What is the problem? There is no difference in formatting between those that work and those 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's what code I'm using to try and 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');

      

The input given to me is always in this format: 06-25-2015 11:30:30 (month day year hr min sec)

+3


source to share


1 answer


This is 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 that the first value appears as May 8th: strtotime treats your dates as dd-mm-yyyy

when you actually have mm-dd-yyyy

.



Notice how the second date appears in December 1969 - how an integer 0

(aka boolean FALSE) appears as a date in my timezone. FALSE for FAILURE because there is no month in the calendar 25

.

Since it is wrong, you will have to do date_create_from_format()

instead, and TELL php, what format your dates are in.

+3


source







All Articles