PHP date error when converting to large int

I want to store two big int dates in a database. I have an array that serializes and sends the values โ€‹โ€‹of the array to the second form, and the array consists of

[checkin] => 01-07-2015
[checkout] => 03-07-2015

      

After converting this value with

$arr['checkin'] = strtotime($arr['checkin']);
$arr['checkout'] = strtotime($arr['checkout']);

      

I get the value as 1435689000 1435861800

respectively, which is one day less than the actual date values.

and just tell my server if my code is

<?php echo date('d-m-y', '1435689000');?>

      

then the output will be 01-07-15

, and if i tried to use the function gmdate

like this

echo gmdate('d-m-y', '1435689000');

      

The output will be 30-06-15

I can't figure out what the problem is: will you help. Thank..

+3


source to share


2 answers


You need to know the difference between both functions

1) date

string date ( string $format [, int $timestamp = time() ] )

      

Returns a string formatted according to the specified format string using the specified integer timestamp, or the current time if no timestamp is specified . In other words, the timestamp is optional and the default is time ().

2) gmdate

string gmdate ( string $format [, int $timestamp = time() ] )

      



Identical to the date() function

except that the time returned is Greenwich Mean Time (GMT) .

If you've seen another function, it already detects that both are identical, but gmdate returns it in GMT

If you repeat both of these functions together with time

, you will understand the difference.

echo date('Y-m-d H:i:sP',1435689000);//2015-07-01 00:00:00+05:30

echo gmdate('Y-m-d H:i:sP',1435689000);//2015-06-30 18:30:00+00:00

      

because according to the GMT time zone your current time is +0530 ahead of GMT . So the output for both is correct, but the difference is timezones

+2


source


We must set the date format before using the strtotime function. Because all the format the user gave cannot be accepted by php. We can use the following:



$date = \DateTime::createFromFormat("d-m-Y" , '03-07-2015');
echo gmdate('d-m-y', strtotime($date->format('Y-m-d H:i:s')));

      

0


source







All Articles