Lokang 

PHP and MySQL

DateTime

Using the DateTime class in PHP provides a more object-oriented way to handle dates and times. Here is an example of how to get the current date using the DateTime class:

<?php
$date = new DateTime();
echo $date->format('Y-m-d'); // Outputs the current date in the format: 2024-05-30
?>

In this example, we create a new DateTime object representing the current date and time. We then use the format() method to output the date in the desired format.

Here are some additional examples of how to use the DateTime class to format dates:

To display the date in d/m/Y format:

<?php
$date = new DateTime();
echo $date->format('d/m/Y'); // Outputs: 30/05/2024
?>

To display the date and time:

<?php
$date = new DateTime();
echo $date->format('Y-m-d H:i:s'); // Outputs: 2024-05-30 14:30:00 (example time)
?>

To create a DateTime object for a specific date and time:

<?php
$date = new DateTime('2024-05-30 14:30:00');
echo $date->format('l, F j, Y H:i:s'); // Outputs: Thursday, May 30, 2024 14:30:00
?>

To modify the date and time:

<?php
$date = new DateTime();
$date->modify('+1 day');
echo $date->format('Y-m-d'); // Outputs the date for tomorrow
?>

The DateTime class provides many methods to manipulate and format dates and times, making it a powerful tool for handling date and time in PHP. You can find more details in the PHP documentation for the DateTime class.