Get the App
SLTechnology News&Howtos  ›  Development  › 

How to convert datetime to date in php

Shulou Source: shulou.com Published: 2022-06-03 12:04:57 11月15日 Update

This article mainly shows you "how to achieve datetime to date in php", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to achieve datetime to date in php" this article.

The way to convert datetime to date in php is as follows: 1, execute the new DateTime to get the current date and time; 2, use the format () method to convert the date and time back to the string used for output.

This article operating environment: windows10 system, PHP7, thinkpad T480 computer.

There is a built-in class Date Time in PHP, and maybe many friends don't know much about this class. Let's talk about this class first.

The DateTime class can help us read, write, compare, or calculate dates and times. Of course, in addition to DATETIME, there are many date and time-related functions in PHP, but it provides a good object-oriented interface for most common uses. It can handle the time zone, but this is outside 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 string to an object, or execute a new DateTime to get the current date and time. Use the format () method to convert the date time back to the string used for the output.

Using the DateInterval class, you can use DateTime for calculations. DateTime has methods like add () and sub (), which take DateInterval as parameters. 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 the date difference, use the diff () method. It will return the new DateInterval, which is very easy to display.

Tags: Date time method object content processing help code character string time zone article two event function learning output original same short Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL NVidia Microsoft Docker OPPO Reno