当前位置:网站首页>Time processing class in PHP
Time processing class in PHP
2022-06-13 03:14:00 【wks19891215】
php Several time processing classes in :DateTime,DateTimeZone,DateInterval,DatePeriod.
DateTime : Time class
DateTimeZone: In time zone
DateInterval: A time interval , It is the unit of addition and subtraction of the time object .
DatePeriod: Represents a collection over a period of time . combination DateInterval Time traversal processing in the set .
The following are specific examples of these classes :
<?php
function showTimeObj($msg,$timeObj)
{
// Use a predefined format to convert time strings
if ($msg!=NULL)
{
echo $msg.":";
echo "<br>".$timeObj->format(DATE_W3C)."<br>";
}
else
{
echo $timeObj->format(DATE_ATOM)."<br>";
}
}
try
{
// Set 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 It represents the East eighth district 、 If the time zone is set to 'Asia/Tokyo', Then for +09:00
showTimeObj('current time',$timeObj);
echo "<br>"."<br>";
echo "use DateInterval object to add date time".":"."<br>";
// Plus one day (P=period, necessary )
$timeObj->add(new DateInterval('P1D'));
showTimeObj('current time + 1 day',$timeObj);
// Plus one hour (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: The date in the iteration processing interval
$interval= new DateInterval('P0DT2H');
$start = new DateTime('2008-08-08',$timezones);
$end = new DateTime('2008-08-09',$timezones);
$timeRange = new DatePeriod($start,$interval,$end);
foreach ($timeRange as $hour)
{
showTimeObj(NULL,$hour);
}
?>Here is the result of the above code displayed in the browser :

PS: Two pieces of information are recommended , One is translated by Chinese 《 About php A practical guide to confusing knowledge points 》、 The other is a Laravel Several articles sorted out by the community 《 Best practices 》
边栏推荐
- String: number of substring palindromes
- Vs 2022 new features_ What's new in visual studio2022
- P1048 [noip2005 popularization group] Drug collection
- JVM GC (V)
- 关于复合函数的极限问题
- Install MySQL database
- Simple use of qtreeview of QT (including source code + comments)
- C method parameter: params
- Understanding of intermediatelayergetter
- DDL operation table
猜你喜欢

MySQL index bottom layer (I)

Mp4 playback
![HEAP[xxx.exe]: Invalid address specified to RtlValidateHeap( 0xxxxxx, 0x000xx)](/img/c9/884aa008a185a471dfe252c0756fc1.png)
HEAP[xxx.exe]: Invalid address specified to RtlValidateHeap( 0xxxxxx, 0x000xx)

二叉樹初始化代碼
![Loading process of [JVM series 3] classes](/img/a7/707c5cb95de71d95bf6ad9b2f69afa.jpg)
Loading process of [JVM series 3] classes

2019 - sorting out the latest and most comprehensive IOS test questions (including framework and algorithm questions)

【pytorch 記錄】pytorch的變量parameter、buffer。self.register_buffer()、self.register_parameter()

AAR packaging and confusion
![[JVM Series 7] garbage collector](/img/e5/902de6398359b75183aa1fafcaa7d1.jpg)
[JVM Series 7] garbage collector

Spark UDF instance details
随机推荐
Hash table: least recently used cache
Available types in C #_ Unavailable type_ C double question mark_ C question mark point_ C null is not equal to
C # simple understanding - method overloading and rewriting
Linked list: reverse linked list
Operating principle of JS core EventLoop
HEAP[xxx.exe]: Invalid address specified to RtlValidateHeap( 0xxxxxx, 0x000xx)
Introduction to redis (using redis, common commands, persistence methods, and cluster operations)
QML connecting to MySQL database
[JVM series 8] overview of JVM knowledge points
Spark UDF instance details
Use of jstack
Ijkplayer source code - choose soft decoding or hard decoding
Es and kibana deployment and setup
【pytorch 记录】pytorch的变量parameter、buffer。self.register_buffer()、self.register_parameter()
How to manage the IT R & D department?
Wechat applet obtains the current location (startlocationupdate, onlocationchange, offlocationchange)
Linked list: the entry node of the link in the linked list
Capital digit to number format
Ijkplayer source code --- decode
A personal understanding of interpreted and compiled languages