Reputation: 12047
I have this code below and it outputs
26/11/1949 but when run through the strtotime
it outputs the ephoc
time of 1970
$var = '26/11/1949';
echo $var;
$var = date("d/m/Y", strtotime($var));
echo $var;
More importantly how can I get the rearranged version of dates before and after ephoc?
Upvotes: 0
Views: 199
Reputation: 2698
The problem here is that strtotime() can not recognize your date.
List of date formats acceptable by strtotime() are listed in PHP documentation . Unfortunately the format you use dd/mm/y doesn't match any format listed there. So if you keep using your code you'll have wrong data conversion in most cases.
My suggestion is to parse the date manually, use mktime to get UNIX timestamp from it and then use date to output format date. Optionally you can start using different strtotime() friendly date format if your scripts allow it.
I'd also recomend using DateTime() class especially if you plan to do further manipulation with your date. The you can use DateTime::createFromFormat('d/m/Y') function to het DateTime object from your string.
Upvotes: 0
Reputation: 94672
The reason you are getting a date returned of 1/1/1970 is that strtotime()
is failing and returning false
which converts to 1/1/1970
the first unix epoch date.
These sort of problems all revolve around the fact that the Americans for some obsquire reason start a date with the month and then the day and then the year. Sound ok when you speak it but it makes date convertion a bit of a nightmare because computers, and the rest of the world, use a more logical y/m/d or d/m/y. So PHP assumes it is a Amerian date format if it see's a /
in the date string.
So the date convertion would work if you were to use
$var = '11/26/1949';
echo $var . PHP_EOL;
$var = date("d/m/Y", strtotime($var));
echo $var;
But luckily it will also work if you convert the seperators to -
instead of /
$var = '26/11/1949';
echo $var . PHP_EOL;
$var = str_replace('/', '-', $var);
echo 'Converted date ' . $var . PHP_EOL;
$var = date("d/m/Y", strtotime($var));
echo $var;
Upvotes: 0
Reputation: 212452
See Date Formats
if PHP sees a /
in the string, it assumes US format dates mm/dd/YYYY
and the 11th day of the 26th month isn't valid
UK format dates need -
rather than /
$var = date("d/m/Y", strtotime(str_replace('/', '-',$var)));
But better to use DateTime objects anyway
Upvotes: 0
Reputation: 781731
The problem is that 26/11/1949
is not a date in a format that strtotime()
recognizes. When the parts of a date are separated by /
, it expects it to be m/d/Y
, but 26
is not a valid month number.
You should use Date::create_from_format
so you can specify how $var
should be parsed:
$date = Date::create_from_format('d/m/Y', $var);
Upvotes: 1
Reputation: 22656
strtotime
returns the number of seconds since the epoch. However from the docs:
Additionally, not all platforms support negative timestamps, therefore your date range may be limited to no earlier than the Unix epoch. This means that e.g. dates prior to Jan 1, 1970 will not work on Windows, some Linux distributions, and a few other operating systems. PHP 5.1.0 and newer versions overcome this limitation though
As an alternative if you know the format I would recommend creating a date from that format:
$var = '26/11/1949';
echo $var;
$date = DateTime::createFromFormat('d/m/Y');
$var = $date->format('d/m/Y');
echo $var;
Upvotes: 0
Reputation: 12246
Because:
The unix time stamp is a way to track time as a running total of seconds. This count starts at the Unix Epoch on January 1st, 1970 at UTC. Therefore, the unix time stamp is merely the number of seconds between a particular date and the Unix Epoch. It should also be pointed out (thanks to the comments from visitors to this site) that this point in time technically does not change no matter where you are located on the globe. This is very useful to computer systems for tracking and sorting dated information in dynamic and distributed applications both online and client side.
Source: http://www.unixtimestamp.com/index.php
Upvotes: 0