Php 日期差
我有两个字符串格式的日期,我想从这两个日期字符串计算这些东西。 1年差额 比如说Php 日期差,php,Php,我有两个字符串格式的日期,我想从这两个日期字符串计算这些东西。 1年差额 比如说 $date1 = "20/04/2002"; $date2= "20/04/2010"; $five_yers_back = <Five years back that is 2005> 同样地 $date2= "20/05/2010"; $a_week = "< seven days back from date2 >"; Plz帮助使用strotime+date http://pl2
$date1 = "20/04/2002";
$date2= "20/04/2010";
$five_yers_back = <Five years back that is 2005>
同样地
$date2= "20/05/2010";
$a_week = "< seven days back from date2 >";
Plz帮助使用strotime+date
http://pl2.php.net/manual/en/function.strtotime.php
http://pl2.php.net/manual/en/function.date.php
我肯定会有课程为你做这件事。但作为一名C程序员,我更喜欢tm结构的基础知识。这里的PHP等效示例
<?php
$d1 = "20/04/2002";
$d1rec = strptime( $d1, "%d/%m/%Y" );
$sec = 0;
$min = 0;
$hour = 0;
$day = $d1rec["tm_mday"];
$mon = $d1rec["tm_mon"] + 1; # Because tm_mon is 0-11
$year = $d1rec["tm_year"];
print( "DATE: " . strftime( "%d/%m/%Y\n"
, mktime($hour,$min,$sec,$mon,$day,$year) ) );
print( "+1WK: " . strftime( "%d/%m/%Y\n"
, mktime($hour,$min,$sec,$mon,$day+7,$year) ) );
print( "+2WK: " . strftime( "%d/%m/%Y\n"
, mktime($hour,$min,$sec,$mon,$day+14,$year) ) );
print( "+1YR: " . strftime( "%d/%m/%Y\n"
, mktime($hour,$min,$sec,$mon,$day,$year+1) ) );
print( "-6MO: " . strftime( "%d/%m/%Y\n"
, mktime($hour,$min,$sec,$mon-6,$day,$year) ) );
?>
所以基本上
使用strtime将字符串解析为结构数组
将数组分解为变量
使用mktime将修改后的变量重新组合为时间整数,并进行月/年包装
使用strftime返回字符串