Rene
Rene

Reputation: 642

Date format conversion issue

I have an issue that I have been dealing for way to long

Please look at the code below: ( I have commented on the right the values that I get when I debug the variable)

When input is for example "0000-00-00 00:00:00"

 $date1 = ($date['confirmed_at']); //value of $date1 is "0000-00-00 00:00:00"
 $date2 = strtotime($date1); //value of $date2 is "1"
 $date3 = date('y-m-d',$date2); //value of $date3 is "70-01-01"

When input is for example "2011-09-12 08:57:26"

 $date1 = ($date['confirmed_at']); //value of $date1 is "2011-09-12 08:57:26"
 $date2 = strtotime($date1); //value of $date2 is "1315817846"
 $date3 = date('y-m-d',$date2); //value of $date3 is "11-09-12"

I'm a bit confused with the values that I have been getting... All that I am trying to do is take the date which is currently in a "YYYY-MM-DD HH-MM-SS" and convert it into a "YYYY-MM-DD" format.

Upvotes: 0

Views: 495

Answers (3)

xdazz
xdazz

Reputation: 160833

The result of strtotime("0000-00-00 00:00:00") may varies by PHP version and OS, so use strtotime for zero date "0000-00-00 00:00:00" makes no sense.

Upvotes: 0

Mchl
Mchl

Reputation: 62369

What you're seeing in the first example results from the fact, that strtotime() returns a unix timestamp, that is a number of seconds since Jan 1st, 1970. It just can't return anything sensible for 0000-00-00 00:00:00.

For YYYY-MM-DD format use date('Y-m-d')

Upvotes: 1

Parag Chaure
Parag Chaure

Reputation: 3005

use "date('Y-m-d',$date2)" for the YYYY-MM-DD format

Upvotes: 0

Related Questions