<?php
Date_default_timezone_set (' PRC ');//default time zone
echo "Today:", Date ("Y-m-d", Time ()), "<br>";
Echo "Today:", Date ("Y-m-d", Strtotime ("June 2008")), "<br > ";
Echo yesterday:", Date ("Y-m-d", Strtotime ("-1 Day"), "<br>";
Echo "Tomorrow:", Date ("y-m-d", Strtotime ("+1 Day"), "<br>";
Echo after one week: ", Date (" Y-m-d ", Strtotime (" +1 Week "))," <br> ";
echo "2 days a week four hours two seconds later:", Date ("Y-m-d g:h:s", Strtotime ("+1 Week 2 days 4 hours 2 Seconds"), "<br>";
Echo "Next Thursday: ", Date (" Y-m-d ", Strtotime (" next Thursday "))," <br> ";
Echo" Last Monday: ". Date (" Y-m-d ", Strtotime (" Previous Monday "))." <br> ";
Echo one months ago:". Date ("Y-m-d", Strtotime ("Last month"). " <br> ";
Echo one months later:". Date ("Y-m-d", Strtotime ("+1 Month")). " <br> ";
Echo ten years later:". Date ("Y-m-d", Strtotime ("+10 Year")). " <br> ";
?>
When learning PHP, it is often used to get the date before or after a certain time period. The collection is now in place, and you can also expand the rich
Get the day of the Week (1-7)
function Getweek ($times)
{
$res = Date (' W ', Strtotime ($times));
if ($res ==0)
$res = 7;
return $res;
}
Get Day time
function GetTime ($times)
{
$res = Date (' H:i ', Strtotime ($times));
return $res;
}
Get the time of the month now
function getmonth ($Month, $type = ' l ')
{
if (!strcmp ($type, ' B '))
$res =date ("y-m-d h:i:s", Strtotime ("-$Month months"));
if (!strcmp ($type, ' l '))
$res =date ("y-m-d h:i:s", Strtotime ("+ $Month months"));
return $res;
}
Get current time
function GetCurrentDateTime ()
{
$res =date ("y-m-d h:i:s", Time ());
return $res;
}
Gets the time before or after the current time interval
function Getdiffhours ($hours, $type = ' l ')
{
if (!strcmp ($type, ' B '))
$res =date ("y-m-d h:i:s", Strtotime ("-$hours Hour"));
if (!strcmp ($type, ' l '))
$res =date ("y-m-d h:i:s", Strtotime ("+ $hours Hour"));
return $res;
}
Time before or after a few minutes interval
function Getdiffminute ($Minute, $type = ' l ')
{
if (!strcmp ($type, ' B '))
$res =date ("y-m-d h:i:s", Strtotime ("-$Minute Minute"));
if (!strcmp ($type, ' l '))
$res =date ("y-m-d h:i:s", Strtotime ("+ $Minute Minute"));
return $res;
}
Time before or after a few seconds interval
function Getdiffsec ($sec, $type = ' l ')
{
if (!strcmp ($type, ' B '))
$res =date ("y-m-d h:i:s", Strtotime ("-$sec Second"));
if (!strcmp ($type, ' l '))
$res =date ("y-m-d h:i:s", Strtotime ("+ $sec Second"));
return $res;
}
A few weeks before or after the interval.
function Getdiffweek ($Week, $type = ' l ')
{
if (!strcmp ($type, ' B '))
$res =date ("y-m-d h:i:s", Strtotime ("-$Week Week"));
if (!strcmp ($type, ' l '))
$res =date ("y-m-d h:i:s", Strtotime ("+ $Week Week"));
return $res;
}
Time interval between days
function Getdiffdays ($days, $type = ' l ')
{
if (!strcmp ($type, ' B '))
$res =date ("y-m-d h:i:s", Strtotime ("-$days Day"));
if (!strcmp ($type, ' l '))
$res =date ("y-m-d h:i:s", Strtotime ("+ $days Day"));
return $res;
}
Time before or after a few years
function Getdiffyears ($year, $type = ' l ')
{
if (!strcmp ($type, ' B '))
$res =date ("y-m-d h:i:s", Strtotime ("-$year Year"));
if (!strcmp ($type, ' l '))
$res =date ("y-m-d h:i:s", Strtotime ("+ $year Year"));
return $res;
}