-1

This code was working fine until yesterday:

$fechaactual = strtotime('now');

$timestamp = strtotime($v->startDate);

$pelo = date("d/m G:i:s",$fechaactual);
$pelo2 = date("m/d G:i:s",$timestamp);

$timestamp = strtotime($pelo2);
$timestamp2 = strtotime($pelo);

Now, $timestamp2 is always empty. I do not understand. Why?

2 Answers2

1

You're attempting to use strtotime() multiple times.

The issue can be reproduced with the code below:

$fechaactual = strtotime('now');
$pelo = date("d/m G:i:s",$fechaactual);
$timestamp2 = strtotime($pelo);
var_dump($timestamp2);

Outputs:

bool(false)

$fechaactual will contain the timestamp for the current time, and by using date(), you're converting the timestamp into a readable format. Later, you're trying to use strtotime() on the new date string, but strtotime() fails to understand that format and returns FALSE.

strtotime() understands the formats listed in this page: http://php.net/manual/en/datetime.formats.date.php

If you want to parse a date string with a custom format, it's better to use DateTime::createFromFormat() instead:

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
  • 1
    The error is more subtle than that. `strtotime()` would return a timestamp until 12th October as it saw `12/10`, and interpreted that as 10th December. On 13th October strtotime saw `13/10` which it can no longer interpret (month 13 is invalid), and returned `false` –  Oct 13 '13 at 14:43
0

Not 100% about this but the fact it worked till yesterday may mean its formatting issue.

Today is 13/10/2013 in UK format. In US that would equate to the 10th day of the 13th month which isn't a valid date... Yesterday would have equated to 10th day of 12th month which would be a valid date although not the date you were expecting.

Gavin
  • 2,123
  • 1
  • 15
  • 19