Home>Article>Backend Development> How to convert datetime to date in php

How to convert datetime to date in php

王林
王林 Original
2021-09-17 13:44:35 2381browse

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.

How to convert datetime to date in php

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.

How to convert datetime to date in php

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.

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:

How to convert datetime to date in php

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.

How to convert datetime to date in php

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

How to convert datetime to date in php

All of this comes in a very useful package that makes doing time processing in PHP very easy.

How to convert datetime to date in php

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn