6 个解决方案
#1
减一日
date("Y-m-d",strtotime("-1 day"));
减一月
date("Y-m-d",strtotime("-1 month"));
date("Y-m-d",strtotime("-1 day"));
减一月
date("Y-m-d",strtotime("-1 month"));
#2
//昨天
$yesterday = Date("Y-m-d",mktime(0,0,0,Date("m"),Date("d")-1,Date("Y")));
//上月
$lastmonth = Date("m",mktime(0,0,0,Date("m")-1,0,0));
$yesterday = Date("Y-m-d",mktime(0,0,0,Date("m"),Date("d")-1,Date("Y")));
//上月
$lastmonth = Date("m",mktime(0,0,0,Date("m")-1,0,0));
#3
<?php
function ch_strtotime($d) {
$ar = array(
array("/今天|现在/","now "),
array("/昨|上|去/","-1 "),
array("/明|下/","+1 "),
array("/^前/","-2 "),
array("/^后/","+2 "),
array("/年/"," year "),
array("/月/"," month "),
array("/周|星期/"," week "),
array("/天|日/"," day"),
array("/小时/"," hours "),
array("/分/"," minutes "),
array("/秒/"," seconds "),
array("/(.+)后/","-\\1"),
array("/(.+)前/","-\\1")
);
for($i=0;$i<count($ar);$i++) {
$patterns[$i] = $ar[$i][0];
$replace[$i] = $ar[$i][1];
}
$d = preg_replace($patterns,$replace,$d);
echo "<br>",date("Y-m-d H:i:s",strtotime($d));
}
// 例
ch_strtotime("昨天");
ch_strtotime("上月");
ch_strtotime("现在");
ch_strtotime("去年");
ch_strtotime("1小时3分40秒");
?>
function ch_strtotime($d) {
$ar = array(
array("/今天|现在/","now "),
array("/昨|上|去/","-1 "),
array("/明|下/","+1 "),
array("/^前/","-2 "),
array("/^后/","+2 "),
array("/年/"," year "),
array("/月/"," month "),
array("/周|星期/"," week "),
array("/天|日/"," day"),
array("/小时/"," hours "),
array("/分/"," minutes "),
array("/秒/"," seconds "),
array("/(.+)后/","-\\1"),
array("/(.+)前/","-\\1")
);
for($i=0;$i<count($ar);$i++) {
$patterns[$i] = $ar[$i][0];
$replace[$i] = $ar[$i][1];
}
$d = preg_replace($patterns,$replace,$d);
echo "<br>",date("Y-m-d H:i:s",strtotime($d));
}
// 例
ch_strtotime("昨天");
ch_strtotime("上月");
ch_strtotime("现在");
ch_strtotime("去年");
ch_strtotime("1小时3分40秒");
?>
#4
function F_between_days($m_start,$m_days,$m_adjust){
if($m_adjust=='+'){
$m_seconds=@strtotime($m_start)+$m_days*24*3600;
}else{
$m_seconds=@strtotime($m_start)-$m_days*24*3600;
}
return $m_seconds;
}
//昨天就是 m_start=今天日期,$m_days=1,$m_adjust='-'
//上个月 $m_days=32就可以了
得到的值,再通过date("Ymd",$m_seconds)的形式来获得日期。"Y-m-d"等等为格式。
if($m_adjust=='+'){
$m_seconds=@strtotime($m_start)+$m_days*24*3600;
}else{
$m_seconds=@strtotime($m_start)-$m_days*24*3600;
}
return $m_seconds;
}
//昨天就是 m_start=今天日期,$m_days=1,$m_adjust='-'
//上个月 $m_days=32就可以了
得到的值,再通过date("Ymd",$m_seconds)的形式来获得日期。"Y-m-d"等等为格式。
#5
function DateAdd ($interval, $number, $date) {
$date_time_array = getdate($date);
$hours = $date_time_array["hours"];
$minutes = $date_time_array["minutes"];
$seconds = $date_time_array["seconds"];
$month = $date_time_array["mon"];
$day = $date_time_array["mday"];
$year = $date_time_array["year"];
switch ($interval) {
case "yyyy": $year +=$number; break;
case "q": $month +=($number*3); break;
case "m": $month +=$number; break;
case "y":
case "d":
case "w": $day+=$number; break;
case "ww": $day+=($number*7); break;
case "h": $hours+=$number; break;
case "n": $minutes+=$number; break;
case "s": $seconds+=$number; break;
}
$timestamp = mktime($hours ,$minutes, $seconds,$month ,$day, $year);
return $timestamp;
}
Function DateDiff ($interval, $date1,$date2) {
// 得到两日期之间间隔的秒数
$timedifference = $date2 - $date1;
switch ($interval) {
case "w": $retval = bcdiv($timedifference ,604800); break;
case "d": $retval = bcdiv( $timedifference,86400); break;
case "h": $retval = bcdiv ($timedifference,3600); break;
case "n": $retval = bcdiv( $timedifference,60); break;
case "s": $retval = $timedifference; break;
}
return $retval;
}
$date_time_array = getdate($date);
$hours = $date_time_array["hours"];
$minutes = $date_time_array["minutes"];
$seconds = $date_time_array["seconds"];
$month = $date_time_array["mon"];
$day = $date_time_array["mday"];
$year = $date_time_array["year"];
switch ($interval) {
case "yyyy": $year +=$number; break;
case "q": $month +=($number*3); break;
case "m": $month +=$number; break;
case "y":
case "d":
case "w": $day+=$number; break;
case "ww": $day+=($number*7); break;
case "h": $hours+=$number; break;
case "n": $minutes+=$number; break;
case "s": $seconds+=$number; break;
}
$timestamp = mktime($hours ,$minutes, $seconds,$month ,$day, $year);
return $timestamp;
}
Function DateDiff ($interval, $date1,$date2) {
// 得到两日期之间间隔的秒数
$timedifference = $date2 - $date1;
switch ($interval) {
case "w": $retval = bcdiv($timedifference ,604800); break;
case "d": $retval = bcdiv( $timedifference,86400); break;
case "h": $retval = bcdiv ($timedifference,3600); break;
case "n": $retval = bcdiv( $timedifference,60); break;
case "s": $retval = $timedifference; break;
}
return $retval;
}
#6
感谢,感谢!
#1
减一日
date("Y-m-d",strtotime("-1 day"));
减一月
date("Y-m-d",strtotime("-1 month"));
date("Y-m-d",strtotime("-1 day"));
减一月
date("Y-m-d",strtotime("-1 month"));
#2
//昨天
$yesterday = Date("Y-m-d",mktime(0,0,0,Date("m"),Date("d")-1,Date("Y")));
//上月
$lastmonth = Date("m",mktime(0,0,0,Date("m")-1,0,0));
$yesterday = Date("Y-m-d",mktime(0,0,0,Date("m"),Date("d")-1,Date("Y")));
//上月
$lastmonth = Date("m",mktime(0,0,0,Date("m")-1,0,0));
#3
<?php
function ch_strtotime($d) {
$ar = array(
array("/今天|现在/","now "),
array("/昨|上|去/","-1 "),
array("/明|下/","+1 "),
array("/^前/","-2 "),
array("/^后/","+2 "),
array("/年/"," year "),
array("/月/"," month "),
array("/周|星期/"," week "),
array("/天|日/"," day"),
array("/小时/"," hours "),
array("/分/"," minutes "),
array("/秒/"," seconds "),
array("/(.+)后/","-\\1"),
array("/(.+)前/","-\\1")
);
for($i=0;$i<count($ar);$i++) {
$patterns[$i] = $ar[$i][0];
$replace[$i] = $ar[$i][1];
}
$d = preg_replace($patterns,$replace,$d);
echo "<br>",date("Y-m-d H:i:s",strtotime($d));
}
// 例
ch_strtotime("昨天");
ch_strtotime("上月");
ch_strtotime("现在");
ch_strtotime("去年");
ch_strtotime("1小时3分40秒");
?>
function ch_strtotime($d) {
$ar = array(
array("/今天|现在/","now "),
array("/昨|上|去/","-1 "),
array("/明|下/","+1 "),
array("/^前/","-2 "),
array("/^后/","+2 "),
array("/年/"," year "),
array("/月/"," month "),
array("/周|星期/"," week "),
array("/天|日/"," day"),
array("/小时/"," hours "),
array("/分/"," minutes "),
array("/秒/"," seconds "),
array("/(.+)后/","-\\1"),
array("/(.+)前/","-\\1")
);
for($i=0;$i<count($ar);$i++) {
$patterns[$i] = $ar[$i][0];
$replace[$i] = $ar[$i][1];
}
$d = preg_replace($patterns,$replace,$d);
echo "<br>",date("Y-m-d H:i:s",strtotime($d));
}
// 例
ch_strtotime("昨天");
ch_strtotime("上月");
ch_strtotime("现在");
ch_strtotime("去年");
ch_strtotime("1小时3分40秒");
?>
#4
function F_between_days($m_start,$m_days,$m_adjust){
if($m_adjust=='+'){
$m_seconds=@strtotime($m_start)+$m_days*24*3600;
}else{
$m_seconds=@strtotime($m_start)-$m_days*24*3600;
}
return $m_seconds;
}
//昨天就是 m_start=今天日期,$m_days=1,$m_adjust='-'
//上个月 $m_days=32就可以了
得到的值,再通过date("Ymd",$m_seconds)的形式来获得日期。"Y-m-d"等等为格式。
if($m_adjust=='+'){
$m_seconds=@strtotime($m_start)+$m_days*24*3600;
}else{
$m_seconds=@strtotime($m_start)-$m_days*24*3600;
}
return $m_seconds;
}
//昨天就是 m_start=今天日期,$m_days=1,$m_adjust='-'
//上个月 $m_days=32就可以了
得到的值,再通过date("Ymd",$m_seconds)的形式来获得日期。"Y-m-d"等等为格式。
#5
function DateAdd ($interval, $number, $date) {
$date_time_array = getdate($date);
$hours = $date_time_array["hours"];
$minutes = $date_time_array["minutes"];
$seconds = $date_time_array["seconds"];
$month = $date_time_array["mon"];
$day = $date_time_array["mday"];
$year = $date_time_array["year"];
switch ($interval) {
case "yyyy": $year +=$number; break;
case "q": $month +=($number*3); break;
case "m": $month +=$number; break;
case "y":
case "d":
case "w": $day+=$number; break;
case "ww": $day+=($number*7); break;
case "h": $hours+=$number; break;
case "n": $minutes+=$number; break;
case "s": $seconds+=$number; break;
}
$timestamp = mktime($hours ,$minutes, $seconds,$month ,$day, $year);
return $timestamp;
}
Function DateDiff ($interval, $date1,$date2) {
// 得到两日期之间间隔的秒数
$timedifference = $date2 - $date1;
switch ($interval) {
case "w": $retval = bcdiv($timedifference ,604800); break;
case "d": $retval = bcdiv( $timedifference,86400); break;
case "h": $retval = bcdiv ($timedifference,3600); break;
case "n": $retval = bcdiv( $timedifference,60); break;
case "s": $retval = $timedifference; break;
}
return $retval;
}
$date_time_array = getdate($date);
$hours = $date_time_array["hours"];
$minutes = $date_time_array["minutes"];
$seconds = $date_time_array["seconds"];
$month = $date_time_array["mon"];
$day = $date_time_array["mday"];
$year = $date_time_array["year"];
switch ($interval) {
case "yyyy": $year +=$number; break;
case "q": $month +=($number*3); break;
case "m": $month +=$number; break;
case "y":
case "d":
case "w": $day+=$number; break;
case "ww": $day+=($number*7); break;
case "h": $hours+=$number; break;
case "n": $minutes+=$number; break;
case "s": $seconds+=$number; break;
}
$timestamp = mktime($hours ,$minutes, $seconds,$month ,$day, $year);
return $timestamp;
}
Function DateDiff ($interval, $date1,$date2) {
// 得到两日期之间间隔的秒数
$timedifference = $date2 - $date1;
switch ($interval) {
case "w": $retval = bcdiv($timedifference ,604800); break;
case "d": $retval = bcdiv( $timedifference,86400); break;
case "h": $retval = bcdiv ($timedifference,3600); break;
case "n": $retval = bcdiv( $timedifference,60); break;
case "s": $retval = $timedifference; break;
}
return $retval;
}
#6
感谢,感谢!