Reputation: 57
I'm getting a weird date ('01/01/1970 00:00:00) when I run the following code in php:
$now = date('d/m/Y H:i:s', time());
$exp = date('d/m/Y H:i:s', strtotime($now .'+100 years'));
Upvotes: 2
Views: 4618
Reputation: 458
You're apparently falling into one or both the following problems:
/
as separator, strtotime
interprets days as months and vice-versa; as you can read in the documentation page for strtotime
(Notes paragraph):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; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed
This can be easily fixed by changing your first line from:
$now = date('d\m\Y H:i:s', time());
to:
$now = date('d-m-Y H:i:s', time());
The valid range of a timestamp is typically from Fri, 13 Dec 1901 20:45:54 UTC to Tue, 19 Jan 2038 03:14:07 UTC. (These are the dates that correspond to the minimum and maximum values for a 32-bit signed integer)
If that's your case, you better use DateTime objects (as others suggested here), which do not suffer this limitation. Here's a line which should work:
$exp = date_format(date_create("+100 years"), 'd/m/Y H:i:s');
What is happening in your code is that:
strtotime
returns false
false
to the second argument of the date
function, which expects an int
, false
is cast to 0
, which makes date
return the Epoch Time.Upvotes: 0
Reputation: 1870
"The valid range of a timestamp is typically from Fri, 13 Dec 1901 20:45:54 UTC to Tue, 19 Jan 2038 03:14:07 UTC. (These are the dates that correspond to the minimum and maximum values for a 32-bit signed integer.)"
http://php.net/manual/en/function.strtotime.php
Upvotes: 0
Reputation: 785
It because you're giving the wrong date format for this function strtotime($now .'+100 years')
and it returns false
.
try that:
echo date('d/m/Y H:i:s', strtotime("+100 year"));
Upvotes: 0
Reputation: 192
Try following one.
$now = date('d/m/Y H:i:s', strtotime('+100 years'));
Output => 26/06/2117 18:58:07
Upvotes: 1
Reputation: 1668
Try this
date('d/m/Y H:i:s', strtotime('+100 years'));
Output :-
26/06/2117 09:31:15
Upvotes: 0
Reputation: 3424
Try this code :
<?php
$year = date('Y-m-d H:i:s', strtotime('+5 years'));
echo $year;
?>
Output:
2022-06-26 13:29:07
Upvotes: 3
Reputation: 146
I would try something like this
$date = new DateTime();
$exp = $date->modify('+100 years')->format('d/m/Y H:i:s');
Upvotes: 6