php convert a date to a timestamp

date, php, string, time, timestamp

Solution

Always use four-digit years, as using two-digit years leads to endless headaches. `13` is not treated as a year; instead, `01-12-13` and `28-11-13` are interpreted as December 13, 2001 and November 13, 2028 by PHP. See PHP's Date Formats page, under "ISO8601 Notations": "Two digit year, month and day with dashes".

If you use `2013` instead, the issues disappear:

$today = date("d-m-Y");  // 01-12-2013
$start_date = "28-11-2013";

$todaytimestamp = strtotime($today);
$starttimestamp = strtotime($start_date);

echo "$today > $start_date <br>$todaytimestamp > $starttimestamp";

Output:

01-12-2013 > 28-11-2013
1385884800 > 1385625600

Problem

I'm trying to convert 2 dates to timestamps. One is generated by php (today's date) and the other is input by the user ``` $today = date("d-m-y"); // 01-12-13 $start_date = "28-11-13"; $todaytimestamp = strtotime($today); $starttimestamp = strtotime($start_date); echo "$today > $start_date <br>$todaytimestamp > $starttimestamp"; ``` Problem is that the result are incorrect Result: 01-12-13 > 28-11-13 1008201600 > 1857686400 what's wrong ?

Original source