我想用php减去两个日期。 Dateone存储在数据库中,而datetwo是当前日期。 现在,我有这个奇怪的场景: Dateone是23-03-13 日期二是13年2月4日Php用不同的日期减去两个日期返回错误的结果
采用不同的方法减,得出不同的结果。
方法一 - 返回-21
$sqldate ="SELECT exam_date FROM exam_table";
$fetchdate = mysql_query($sqldate);
$rowdate = mysql_fetch_array($fetchdate);
//Fetch the date stored in the database
$dateone = $rowdate['exam_date'];
//Calculate the current date today
$datetwo =date('d-m-y');
//Calculate the diff between the two dates
$datediff = $datetwo-$dateone;
在这种情况下,$ DATEDIFF返回-21
方法二 - 返回-7639
$sqldate ="SELECT exam_date FROM exam_table";
$fetchdate = mysql_query($sqldate);
$rowdate = mysql_fetch_array($fetchdate);
//Fetch the date stored in the database
$dateone = $rowdate['exam_date'];
//Calculate the current date
$datetwo =date('d-m-y');
//Calculate the diff between the two dates
$datetime1 = strtotime("$dateone");
$datetime2 = strtotime("$datetwo");
//seconds between the two times
$secs = $datetime2 - $datetime1;
$days = $secs/86400;
在这种情况下,$天返回-7639
我假设其中一个日期比另一个大。在运行时打印它们并查看。 – MahanGM
在第二个中,您使用$ datetoday,但转换为使用$ datetwo的时间。请在运行时写出$ dateone的结果,然后写出$ datetwo的结果,以便我们可以真正看到结果。 – karmafunk
**方法二**中的$ datetwo'在哪里? – egig