PHP timestamp functions in a detailed

Source: Internet
Author: User
Tags echo date
This article is mainly to share with you PHP timestamp function in detail, mainly in the form of code and we share, hope to help everyone.

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

2. Convert timestamps to System time

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

(1) Get time stamp of 0 points on the day

$timetoday = Strtotime (Date ("Y-m-d", Time ()));

(2) Get a 0 time stamp tomorrow.

$tomorrow = $timetoday + 3600*24;

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 current time is echo date ("Y-m-d h:i:s", Time ()), or//tomorrow at this moment echo date ("Y-m-d h:i:s", Strtotime ("+1 Day"));

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

The current time echo date ("Y-m-d h:i:s", Time ()), or//Specify the date echo date ("Y-m-d h:i:s", Strtotime ("1 day"));

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

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

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

The current time is echo date ("Y-m-d h:i:s", Time ());//Last week at this moment echo date ("Y-m-d h:i:s", Strtotime ("1 week"));

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

The current time is echo date ("Y-m-d h:i:s", Time ()), and/or the 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

The current time is echo date ("Y-m-d h:i:s", Time ()), or//Specify the date 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

function mdate ($time = NULL) {$text = '; $time = $time = = = NULL | | $time > Time ()?    Time (): Intval ($time); $t = Time ()-$time;       The time difference (in seconds) $y = date (' y ', $time)-date (' Y ', Time ()),//Whether the cross-year switch ($t) {case $t = = 0: $text = ' just ';     Break Case $t <: $text = $t. ' seconds ago ';     Break in a minute; Case $t < *: $text = Floor ($t/60). ' Minutes ago ';     Break within an hour; Case $t < *: $text = Floor ($t/(60 * 60)). ' Hour ago ';     Break in one day; Case $t < * $ * 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 the break; Case $t < * *: $text = date (' m ' D Day h:i ', $time);     Break within one months; Case $t < * 365&& $y ==0: $text = Date (' m ' D Day ', $time);     Break within a year; Default: $text = Date (' Y year m D Day ', $time);    Break a year ago; } 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

{$vo. Time|mdate}

Example 2, a few simple

function FormatDate ($sTime) {//stime= source time, ctime= current time, dtime= difference $cTime  = times (); $dTime  = $cTime-$sTime; $dDay c2/>= intval (Date ("Ymd", $cTime))-Intval (Date ("Ymd", $sTime)); $dYear  = intval (Date ("Y", $cTime))-Intval (date ("Y", $sTime)), if ($dTime <) {  $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;}

Here is a friendly time to send you a method

/** * Friendly time display * @param $time * @return bool|string */function friend_date ($time) {if (! $time) return false;    $fdate = ";    $d = Time ()-intval ($time); $ld = $time-mktime (0, 0, 0, 0, 0, date (' Y ')); The Year $MD = $time-mktime (0, 0, 0, date (' m '), 0, date (' Y ')); Draw the Month $byd = $time-mktime (0, 0, 0, date (' m '), date (' d ')-2, date (' Y ')); The day before yesterday $yd = $time-mktime (0, 0, 0, date (' m '), date (' d ')-1, date (' Y ')); Yesterday $dd = $time-mktime (0, 0, 0, date (' m '), date (' d '), date (' Y ')); Today $TD = $time-mktime (0, 0, 0, date (' m '), date (' d ') + 1, date (' Y ')); Tomorrow $atd = $time-mktime (0, 0, 0, date (' m '), date (' d ') + 2, date (' Y '));    The acquired if ($d = = 0) {$fdate = ' just ';                } else {switch ($d) {case $d < $atd: $fdate = date (' Y year m D Day ', $time);            Break Case $d < $td: $fdate = ' The day After Tomorrow '.                Date (' H:i ', $time);            Break Case $d < 0: $fdate = ' tomorrow '. DatE (' h:i ', $time);            Break Case $d <: $fdate = $d.                ' seconds ago ';            Break Case $d < 3600: $fdate = Floor ($d/60).                ' Minutes ago ';            Break Case $d < $dd: $fdate = Floor ($d/3600).                ' Hour ago ';            Break Case $d < $yd: $fdate = ' Yesterday '.                Date (' H:i ', $time);            Break Case $d < $byd: $fdate = ' The day before yesterday '.                Date (' H:i ', $time);            Break                Case $d < $md: $fdate = Date (' M-D-Day h:i ', $time);            Break                Case $d < $ld: $fdate = Date (' m ' D Day ', $time);            Break                Default: $fdate = Date (' Y year m D Day ', $time);        Break }} return $fdate;}

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.