PHP Timestamp function _php Tutorial

Source: Internet
Author: User
Tags echo date
1.PHP Timestamp function converts date to UNIX timestamp

World Doomsday Timestamp PHP

The code is as follows

echo "Doomsday timestamp:". Strtotime ("2012-12-21")

2. Convert timestamps to System time

The code is as follows


Date (' y-m-d h:i:s ', "1228348800");

3.PHP timestamp function Get the English text date time example is as follows:

Easy to compare, use date to convert time stamp to system time with specified timestamp

(1) Print the timestamp for tomorrow at this time Strtotime ("+1 Day")

The code is as follows

Current time
echo Date ("Y-m-d h:i:s", Time ());
Tomorrow at this time
echo Date ("Y-m-d h:i:s", Strtotime ("+1 Day");

(2) Print the timestamp at this time yesterday Strtotime ("-1 day")

The code is as follows

Current time
echo Date ("Y-m-d h:i:s", Time ());
Specified time
echo Date ("Y-m-d h:i:s", Strtotime ("1 day"));

(3) Print timestamp strtotime ("+1 Week") at this time next week

The code is as follows


Current time
echo Date ("Y-m-d h:i:s", Time ());
Next week time
echo Date ("Y-m-d h:i:s", Strtotime ("+1 Week"));

(4) Print timestamp strtotime ("1 week") at this time last week

The code is as follows

Current time
echo Date ("Y-m-d h:i:s", Time ());
Last week this time
echo Date ("Y-m-d h:i:s", Strtotime ("1 week"));

(5) Print the time stamp specified next week strtotime ("next Thursday")

The code is as follows
Current time
echo Date ("Y-m-d h:i:s", Time ());
A few days next week
echo Date ("Y-m-d h:i:s", Strtotime ("next Thursday"));

(6) Print the timestamp strtotime ("last Thursday") specified on the previous day of the week

code as follows

//Current time
Echo date (" Y-m-d h:i:s ", Time ()),
//Specify the
Echo date (" Y-m-d h:i:s ", Strtotime (" Last Thursday "));

The PHP timestamp Function example above shows that Strtotime can parse the datetime description of any English text into a Unix timestamp, and we get the specified timestamp by combining mktime () or date () to format the date time

Say to see someone else write a function, test it, there are some minor problems: for the cross-year date, not show the year. Modify the following

The code is as follows

function mdate ($time = NULL) {
$text = ";
$time = $time = = = NULL | | $time > Time ()? Time (): Intval ($time);
$t = Time ()-$time; Time difference (seconds)
$y = date (' y ', $time)-date (' Y ', Time ());//whether cross-year
Switch ($t) {
Case $t = = 0:
$text = ' just ';
Break
Case $t < 60:
$text = $t. ' seconds ago '; In a minute.
Break
Case $t < 60 * 60:
$text = Floor ($t/60). ' Minutes ago '; Within one hour
Break
Case $t < 60 * 60 * 24:
$text = Floor ($t/(60 * 60)). ' Hour ago '; In one day
Break
Case $t < 60 * 60 * 24 * 3:
$text = Floor ($time/(60*60*24)) ==1? ' Yesterday '. Date (' H:i ', $time): ' The day before yesterday '. Date (' H:i ', $time); Yesterday and the day before
Break
Case $t < 60 * 60 * 24 * 30:
$text = Date (' m ' D Day h:i ', $time); Within one months
Break
Case $t < * 365&& $y ==0:
$text = Date (' m ' D Day ', $time); One year
Break
Default
$text = Date (' Y year m D Day ', $time); A year ago
Break
}

return $text;
}

In thinkphp:

Place the written function under the Common folder. The system will load automatically.

Put it on this common.php page, common.php the native format, do not rename it.

Called directly in the template

The code is as follows

{$vo. Time|mdate}

Example 2, a few simple

code as follows

function FormatDate ($sTime) {
Stime= source time, ctime= current time, dtime= difference
$cTime = time ();
$dTime = $cTime-$sTime;
$dDay = intval (Date ("Ymd", $cTime))-Intval (Date ("Ymd", $sTime));
$dYear = intval (Date ("Y", $cTime))-Intval (date ("Y", $sTime));
if ($dTime < 60) {
$dTime = $dTime. " Seconds ago ";
}elseif ($dTime < 3600) {
$dTime = Intval ($dTime/60). " Minutes ago ";
}elseif ($dTime >= 3600 && $dDay = = 0) {
$dTime = "Today". Date ("H:i", $sTime);
}elseif ($dYear ==0) {
$dTime = Date ("m-d h:i", $sTime);
}else{
$dTime = Date ("y-m-d h:i", $sTime);
}
return $dTime;
}

http://www.bkjia.com/PHPjc/730235.html www.bkjia.com true http://www.bkjia.com/PHPjc/730235.html techarticle 1.PHP Timestamp function converts a date to a Unix timestamp the doomsday timestamp PHP code is as follows echo Doomsday timestamp:. Strtotime (2012-12-21) 2. Convert timestamp to system time ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.