PHP Time Handling Class Action Sample

  • 2021-11-01 02:26:40
  • OfStack

This article illustrates the PHP time processing class operation. Share it for your reference, as follows:

Several time processing classes in php: DateTime, DateTimeZone, DateInterval, DatePeriod.

DateTime: Time class DateTimeZone: Time Zone DateInterval: Represents a time interval, which is the unit of addition and subtraction of time objects. DatePeriod: Represents a collection of 1 periods of time. Combined with DateInterval, the time traversal in the set can be processed.

The following are specific examples of the use of these classes:


<?php
function showTimeObj($msg,$timeObj)
{
  // Converts a time string using a predefined format 
  if ($msg!=NULL)
  {
    echo $msg.":";
    echo "<br>".$timeObj->format(DATE_W3C)."<br>";
  }
  else
  {
    echo $timeObj->format(DATE_ATOM)."<br>";
  }
}
try
{
  // Setting Time Zone 
  $timezones = new DateTimeZone('Asia/Shanghai');
  // Get the current time 
  $timeObj=new DateTime("now",$timezones);
}
catch (Exception $e)
{
  echo $e->getMessage();
}
// Output +08:00 Representative East 8 Zone, if the time zone is set to 'Asia/Tokyo', Otherwise +09:00
showTimeObj('current time',$timeObj);
echo "<br>"."<br>";
echo "use DateInterval object to add date time".":"."<br>";
// Plus 1 Days (P=period, Necessary )
$timeObj->add(new DateInterval('P1D'));
showTimeObj('current time + 1 day',$timeObj);
// Plus 1 Hours (T=time, Optional )
$timeObj->add(new DateInterval('P0DT1H'));
showTimeObj('current time + 1 day & 1hour',$timeObj);
echo "<br>"."<br>";
echo "use DatePeriod to process time range like a iterator".":"."<br>";
//DatePeriod:  Dates in iterative processing interval 
$interval= new DateInterval('P0DT2H');
$start = new DateTime('2018-08-08',$timezones);
$end = new DateTime('2018-08-09',$timezones);
$timeRange = new DatePeriod($start,$interval,$end);
foreach ($timeRange as $hour)
{
  showTimeObj(NULL,$hour);
}
?>

The above code runs as follows:

current time:
2018-09-04T15:23:46+08:00


use DateInterval object to add date time:
current time + 1 day:
2018-09-05T15:23:46+08:00
current time + 1 day & 1hour:
2018-09-05T16:23:46+08:00


use DatePeriod to process time range like a iterator:
2018-08-08T00:00:00+08:00
2018-08-08T02:00:00+08:00
2018-08-08T04:00:00+08:00
2018-08-08T06:00:00+08:00
2018-08-08T08:00:00+08:00
2018-08-08T10:00:00+08:00
2018-08-08T12:00:00+08:00
2018-08-08T14:00:00+08:00
2018-08-08T16:00:00+08:00
2018-08-08T18:00:00+08:00
2018-08-08T20:00:00+08:00
2018-08-08T22:00:00+08:00

PS: Here are some time and date related tools for your reference:

Online date/day calculator:
http://tools.ofstack.com/jisuanqi/date_jisuanqi

Online date calculator/difference days calculator:
http://tools.ofstack.com/jisuanqi/datecalc

Online Date-Day Difference Calculator:
http://tools.ofstack.com/jisuanqi/onlinedatejsq

Unix timestamp (timestamp) conversion tool:
http://tools.ofstack.com/code/unixtime

For more readers interested in PHP related content, please check the topics on this site: "Summary of php Date and Time Usage", "Encyclopedia of PHP Array (Array) Operation Skills", "Introduction to PHP Basic Syntax", "Summary of PHP Operation and Operator Usage", "Introduction to php Object-Oriented Programming" and "Summary of php String (string) Usage"

I hope this article is helpful to everyone's PHP programming.


Related articles: