The method to convert datetime to date in php is: 1. First execute a new DateTime to obtain the current date and time; 2. Use the format() method to convert the date and time back to a string for output.
The operating environment of this article: windows10 system, PHP7, thinkpad t480 computer.
There is a built-in class Date Time in PHP. Maybe many friends don’t know much about this class. Let’s talk about this class first.
DateTime class can help us read, write, compare or calculate dates and times. Of course there are many date and time related functions in PHP besides DATETIME, but it provides a good object-oriented interface for most common uses. It can handle time zones, but that's beyond the scope of this brief introduction.
If we want to start using DateTime, we need to use the createFromFormat() factory method to convert the original date and time strings into objects, or execute a new DateTime to get the current date and time. Use the format() method to convert the datetime back to a string for output.
Use the DateInterval class to perform calculations using DateTime. DateTime has methods like add() and sub() which take DateInterval as parameter. Don't write code that expects the same number of seconds every day, daylight saving time and time zone changes break this assumption. Use date intervals instead. To calculate date differences, use the diff() method. It will return the new DateInterval, very easy to display.
<?php // 克隆一份 $start ,在其基础上加一个月零6天 $end = clone $start; $end->add(new DateInterval('P1M6D')); $diff = $end->diff($start); echo 'Difference: ' . $diff->format('%m month, %d days (total: %a days)') . "n"; // 差值: 1 个月, 6 天 (总共: 37 天)
For DateTime objects you can use standard comparisons:
The last example demonstrates the DatePeriod class. It is used to iterate over recurring events. It can accept two DateTime objects, Start and End, and return the interval of all events between these two objects.
A popular PHP API extension is Carbon. It inherits everything from the DateTime class, so involves minimal code changes, but additional features include localization support, further methods for adding, subtracting, and formatting DateTime objects, and testing by simulating a date and time of your choice code method.
Carbon provides some great features for working with dates in PHP, specifically things like:
Handling time zones
Get the current time easily
Convert datetime into readable content
Parse English phrases into datetime (first day of January 2016)
Addition and subtraction of dates (2 weeks, -6 months)
Semantic methods for processing dates
All of this comes in a very useful package that makes doing time processing in PHP very easy.
Carbon can do much more than that. Be sure to check out the official Carbon documentation. Hope this helps you make working with dates/times in PHP easier and speed up your development!
Recommended learning: php training
The above is the detailed content of How to convert datetime to date in php. For more information, please follow other related articles on the PHP Chinese website!