PHP timestamp function

Source: Internet
Author: User
Tags current time echo date

1.PHP Timestamp function converts a date to a Unix timestamp

World End Time Stamp PHP

The code is as follows

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

2. Convert time stamp to system time

The code is as follows


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

The 3.PHP timestamp function Gets an example of an English text date time as follows:

Easy to compare, using date to convert the timestamp to the system time with the specified timestamp

(1) Print tomorrow at this time the timestamp 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 strtotime ("-1 day") at this time yesterday

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 the 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 the 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 ());
This time last week
echo Date ("Y-m-d h:i:s", Strtotime ("-1 Week"));

(5) Print a timestamp strtotime ("next Thursday") that specifies the next day of the week

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 a timestamp strtotime ("last Thursday") that specifies the previous week

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 ("last Thursday"));

The above PHP Timestamp function example shows that strtotime can resolve the date-time description of any English text to a Unix timestamp, and we combine the mktime () or date () format date time to get the specified timestamp to achieve the required datetime

To see someone else write a function, test it, there are some minor problems: for a cross year date, no year is displayed. Modified as follows

  code is as follows  

function Mdate ($time = NULL) {
    $text = ';
 & nbsp;  $time = $time = = NULL | | $time > Time ()? Time (): Intval ($time);
    $t = time ()-$time;//Time difference (sec)
    $y = date (' y ', $time)-date (' Y ', Time ()); Cross-year
    switch ($t) {
     case $t = 0:
       $ Text = ' just ';
       break;
     case $t <:
       $text = $t. ' seconds ago '; Within one minute
      break
     case $t <:
  &nbs P    $text = Floor ($t/60). ' Minutes ago '; Within one hour
      break
     case $t < * *:
  & nbsp    $text = Floor ($t/(60 * 60)). ' Hour ago '; Within one day
      break;
     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
      break
     case $t < * * *:
 & nbsp;     $text = Date (' m-month D-Day h:i ', $time); Within one months
      break
     case $t < * * 365&& $y = =0:
       $text = Date (' m month d ', $time);//within one year
      break
     default:
       $text = date (' Y year m month d ', $time);//A year ago
 & nbsp;    break; 
   }
        
    return $text;
}

In thinkphp:

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

Put it in this common.php page, common.php the inherent format, do not rename.

Called directly in the template

The code is as follows

{$vo. Time|mdate}

Example 2, a simple

  code is as follows &nbs P;

function FormatDate ($sTime) {
 //stime= source time, ctime= current time, dTi Me= time lag
  $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 <) {
   $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;
}

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.