2

I have a valid date from a database column with varchar type, all in d/m/y format. Then when I try to convert it into a date format with strtotime function and date formating I get 01/01/70. I have even tried to replace the database result with plain string values. I have searched and found this links PHP strtotime returns a 1970 date when date column is null PHP : strtotime() returns always 01/01/1970 strtotime() does not return correct value when specifying date in dd/mm/yyyy format but both were not helpful since the data is not null and it is in valid format. Here is a snippet of my code

if(isset($item['prep_date']) && $item['prep_date'] != NULL){
    $trial=$item['prep_date'];
    echo ": ".$trial;
    $prepDate= strtotime($trial);
    $prepDate = date('d/m/y', $prepDate);
    echo "= ".$prepDate;
}

What am i missing? Here is the result of the two echoes

28/11/13= 01/01/70: 28/11/13= 01/01/70: 28/11/13= 01/01/70: 03/01/13= 01/03/13: 28/11/13= 01/01/70: 28/11/13= 01/01/70: 18/11/13= 01/01/70: 28/11/13= 01/01/70: 28/11/13= 01/01/70: 02/09/13= 09/02/13: 28/11/13= 01/01/70: 28/11/13= 01/01/70: 28/11/13= 01/01/70: 28/11/13= 01/01/70: 28/11/13= 01/01/70: 28/11/13= 01/01/70: 28/11/13= 01/01/70: 28/11/13= 01/01/70: 28/11/13= 01/01/70: 05/11/13= 11/05/13: 28/10/13= 01/01/70: 28/11/13= 01/01/70: 05/11/13= 11/05/13: 28/10/13= 01/01/70: 28/10/13= 01/01/70: 28/11/13= 01/01/70: 28/11/13= 01/01/70: 05/11/13= 11/05/13: 11/12/13= 12/11/13: 11/12/13= 12/11/13: 28/11/13= 01/01/70: 28/11/13= 01/01/70: 11/12/13= 12/11/13: 28/11/13= 01/01/70: 28/11/13= 01/01/70: 28/11/13= 01/01/70: 28/11/13= 01/01/70: 28/11/13= 01/01/70: 28/11/13= 01/01/70: 28/11/13= 01/01/70: 30/01/14= 01/01/70
Community
  • 1
  • 1
altsyset
  • 339
  • 2
  • 20
  • 3
    can you please echo or var_dump($item['prep_date']) and post the result? – Ryan Oct 27 '13 at 08:37
  • 3
    `28/11/13` is treated as `11th day of 28th month` (and it's surpising for me that it wasn't obvious for you from the `03/01/13= 01/03/13` result). If you want the `day month year` order - use dot, not slash – zerkms Oct 27 '13 at 08:40
  • yes, using a 4 digit year is a really good habit also – Ryan Oct 27 '13 at 08:41
  • @zerkms I specifically gave d/m/y to specify the 28th day of 11th month. How is that possible – altsyset Oct 27 '13 at 08:42
  • 1
    @altsyset: I'm not getting it. "strtotime function return 01/01/70 with valid input" --- is incorrect, there is no 28th month (on this planet). "I specifically gave d/m/y to specify the 28th day of 11th month" --- nope, you're doing it for **FORMATTING**, **AFTER** parsing – zerkms Oct 27 '13 at 08:42
  • @zerkms I will give it a shot with a dot – altsyset Oct 27 '13 at 08:44
  • @altsyset: also try to read your code once again and realize that `date` is performed **AFTER** `strtotime` – zerkms Oct 27 '13 at 08:44
  • if you have php 5.2 try http://www.php.net/manual/en/datetime.format.php – Ryan Oct 27 '13 at 08:46
  • @zerkms that does it...thanks man! – altsyset Oct 27 '13 at 08:51

1 Answers1

5

Your get value of year 1970 because your strtotime() returned false:

var_dump( strtotime('28/11/13') ); # false

Take a look at supported date and time formats. strtotime asumes this is american month, day and year (mm/dd/yyyy), like it states here.

If inputed format is dd/mm/yyyy then the easiest method would be to use DateTime::createFromFormat or strtotime on str_replace('/', '.', 'dd/mm/yyyy'); to make it european date format dd.mm.yyyy.

Glavić
  • 42,781
  • 13
  • 77
  • 107
  • op's is 2 digit year, will DateTime still handle it OK? – Ryan Oct 27 '13 at 08:48
  • 1
    @Ryan: it will even handle 1 digit. See [standard format for american format](http://www.php.net/manual/en/datetime.formats.date.php). Description = `American month, day and year`; format = `mm "/" dd "/" y`; examples = `"12/22/78", "1/17/2006", "1/17/6"`. – Glavić Oct 27 '13 at 08:51
  • this is even simpler solution – altsyset Oct 28 '13 at 07:49