Date does not "respect" PHP formatting

Asked

Viewed 322 times

0

I gave a search and found no solution to the problem, I really think it should be something simple that maybe I’m wrong.

In the system users can insert some files via intranet, when they insert the file they inform the time that this file will become available, this date is saved as int basically when the guy clicks send in the code happens the following: strtotime(date('d/m/Y H:i:s')) that picks up the current date, so far all ok.

I believe that the strotime is not converting the date to a int which corresponds to the current date.

Then I need to recover this data on a given page to check if it is still available, the date comes in format int and I convert to a human date more precisely set with date_default_timezone_set('America/Sao_Paulo'); this line gets in the configuration file that is called in all system files.

Then when I get the dates everything responds normally, but when I get the current date again, it replaces the month by the day, even with the formatted date:

$dataatual   = strtotime(date('d/m/Y H:i:s'));
echo $comparador1 = date('d/m/Y H:i:s',$dataatual);

The result that comes is03/12/2019 10:36:32.

The rest of the code works normally, the error is on the line I get the current date that is in code here above, I’m not understanding what is happening.

Then I take the date that comes from the bank also as int and the conversion occurs normally.

  1. time corresponds to the int coming from the database
  2. tempoV corresponds to a variable that comes with a value to make change in date ex: +1 hour
    
    $datacriado  = strtotime(date('d/m/Y H:i:s', $x['tempo']));
    echo '< br>';
    echo $comparador2 = date('d/m/Y H:i:s',$datacriado);
    $comparador2 = strtotime(date('d/m/Y H:i:s',$datacriado).$x['tempoV']);
    
  • I believe it is because you are doing the date(’d/m/Y H:i:s') twice, ai na primeira ele troca o mês por dia ficando em formato correto, 12/03/2019 e na segunda vez que você faz ele inverte denovo ficando 03/12/2019

  • In your last code, just do $datacriado = $x['tempo']. There’s no reason to turn time into a string and then get the number again...

1 answer

1


The problem is in the first line (strtotime(date('d/m/Y H:i:s'));).

According to the documentation of strtotime:

Dates in the m/d/y or d-m-y formats are disambiguated by Looking at the separator between the Various Components: if the separator is a Slash (/), then the American m/d/y is assumed

That is, if the date is separated by bars, the format is assumed to be month/day/year. Example:

echo strtotime('12/03/2019 10:52:04'); // 1575377524
echo strtotime('03/12/2019 10:52:04'); // 1552398724

The first line returns 1575377524, which corresponds to December 3. The second line returns 1552398724, which corresponds to March 12.

Then it would be enough to reverse the day and month in its format:

strtotime(date('m/d/Y H:i:s'))

But actually all this is unnecessary (convert to string only to get the timestamp then). If you want the timestamp corresponding to the current instant, just use the function time():

$dataatual = time(); // valor do timestamp atual

There is another detail to note in your code, when the day is longer than 12:

// 23 de dezembro
$x = strtotime('23/12/2019 10:52:04');
var_dump($x); // bool(false)

I tried to pass "December 23", but we have seen that strtotime will try to interpret as "day 12 of month 23". Only instead of giving error (since month 23 does not exist), the return is a Boolean (false).

Only if we pass that figure (false) back to date, it is interpreted as the number zero:

echo date('d/m/Y H:i:s', $x);
// é o mesmo que
echo date('d/m/Y H:i:s', 0);

Both produce the date equivalent to the zero instant, which is itself Unix Epoch (January 1, 1970, midnight, at UTC). In the case, the Unix Epoch is converted to Timezone default, so in my case the exit was 31/12/1969 21:00:00.

Therefore, to get the current timestamp it is better to simply use time() (more direct and without having to do/to string conversions). If you need dates other than the current one, just change the day and month in its format.


About your code

$datacriado = strtotime(date('d/m/Y H:i:s', $x['tempo']));

If $x['tempo'] is the numeric value of timestamp, all this process is unnecessary. date will take this value and turn it into a string. Next, strtotime takes the string and turns it back into the numeric value of the timestamp. Why not just do:

$datacriado = $x['tempo'];

Anyway, let’s show that the code is still wrong:

$tempo = 1552398724;
echo date('d/m/Y H:i:s', $tempo); // 12/03/2019 10:52:04

$datacriado = strtotime(date('d/m/Y H:i:s', $tempo));
echo $datacriado; // 1575377524

echo date('d/m/Y H:i:s', $datacriado); // 03/12/2019 10:52:04

Again, see that strtotime interpreted the day as the month and vice versa, generating a timestamp different from the original. The problem is in the format of the string that is passed to strtotime.

  • But is there a reason why this happens only on this date? the other dates receive the same treatment and comes in the expected format, I do not know if I can paste the code here so I’ll give an update on the post just so you understand, but your answer already worked Thanks

  • @Martinsluan Are you sure all the others are working? If you pass d/m/y for strtotime, he will interpret as m/d/y. Maybe the dates that work are using other things (another format, etc), or they are all wrong anyway. Anyway, I updated the answer

  • Paramently it works yes, the result of the last code is this: 12/03/2019 11:06:31 12/03/2019 11:06:31, about

  • With the code that’s in the question I can’t simulate the "works" case. But anyway, I find these PHP date functions somewhat confusing, mainly because of these arbitrary behaviors ("if the separator is slash, then it’s m/d/y", etc), so I suggest using the same answer code :-)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.