DateTime(): Creates date and time objects

$today   = new DateTime;
echo $today->format('Y-m-d H:i:s'); // today's date and time 
The above example will display today's date and time in Year-month-date Hour:minutes:second format. Here is the output
2025-10-17 07:03:01
If you are getting warning message saying not to rely on system time zone , then add this line to the top.
date_default_timezone_set('America/Chicago');
$today   = new DateTime;
echo $today->format('Y-m-d H:i:s');
Let us try differently
date_default_timezone_set('America/Chicago');
$date = new DateTime("now");
echo $date->format('d-m-y H:i:s');
echo "<br><br>";
$date = new DateTime("tomorrow");
echo $date->format('d-m-y H:i:s');
Out put is here
17-10-25 07:03:01

18-10-25 00:00:00
Another way ( object oriented style )
date_default_timezone_set('America/Chicago');
$date = new DateTime('2012-04-15 22:15:40');
echo $date->format('Y-m-d H:i:s') ;
Output is here
2012-04-15 22:15:40
Procedural style
$date = date_create('2018-11-29 23:15:40');
echo date_format($date, 'Y/m/d H:i:s');
2018/11/29 23:15:40

Select Timezone to get current date and time using different formats

Using DateTimeZone

$date = new DateTime('2019-01-01 16:55:58', new DateTimeZone('America/Chicago'));
echo $date->format('Y-m-d H:i:sP'); // Output is 2019-01-01 16:55:58-06:00

Output as ISO 8601 date time format

PHP Script to display date & time in ISO format.
echo date(DateTime::ISO8601);

echo "<br>";
$dt_object = new DateTime('NOW');
echo $dt_object->format('c'); 

echo "<br>";

echo $dt_object->format(DateTime::ISO8601);
Output is here
2025-10-17T07:03:01-0500
2025-10-17T07:03:01-05:00
2025-10-17T07:03:01-0500
Code Output
$d = new DateTime('now');
echo $d->format('Y-m-d') ;
2025-10-17
$d = new DateTime('19 July 2018');
echo $d->format('Y-m-d') ;
2018-07-19
$d = new DateTime('Tomorrow');
echo $d->format('Y-m-d') ;
2025-10-18
$d = new DateTime('Yesterday');
echo $d->format('Y-m-d') ;
2025-10-16
$d = new DateTime('+1 day');
echo $d->format('Y-m-d') ;
2025-10-18
$d = new DateTime('+1 week');
echo $d->format('Y-m-d') ;
2025-10-24
$d = new DateTime('+1 week 5 days');
echo $d->format('Y-m-d') ;
2025-10-29
$d = new DateTime('+1 month');
echo $d->format('Y-m-d') ;
2025-11-17
$d = new DateTime('+1 month 10 days');
echo $d->format('Y-m-d') ;
2025-11-27
$d = new DateTime('+3 months -1 day');
echo $d->format('Y-m-d') ;
2026-01-16
$d = new DateTime('+3 years');
echo $d->format('Y-m-d') ;
2028-10-17
$d = new DateTime('+1 year 5 days');
echo $d->format('Y-m-d') ;
2026-10-22
$d = new DateTime('Monday');
echo $d->format('Y-m-d') ;
2025-10-20
$d = new DateTime('Monday +1 day ');
echo $d->format('Y-m-d') ;
2025-10-21
$d = new DateTime('first day of this month');
echo $d->format('Y-m-d') ;
2025-10-01
$d = new DateTime('last day of this month');
echo $d->format('Y-m-d') ;
2025-10-31
$d = new DateTime('first day of previous month');
echo $d->format('Y-m-d') ;
2025-09-01
$d = new DateTime('last day of previous month');
echo $d->format('Y-m-d') ;
2025-09-30
$d = new DateTime('first day of next month');
echo $d->format('Y-m-d') ;
2025-11-01
$d = new DateTime('last day of next month');
echo $d->format('Y-m-d') ;
2025-11-30
$d = new DateTime('last day of next month +1 hour');
echo $d->format('Y-m-d : H:i:s') ;
2025-11-30 : 08:03:01
$d = new DateTime('Monday +1 hour +20 minutes +10 seconds');
echo $d->format('Y-m-d : H:i:s') ;
2025-10-20 : 01:20:10
$d = DateTime::createFromFormat('d-m-Y', '11-06-2018');
$d = $d->modify('next monday');
echo $d->format('Y-m-d') ;
2018-06-18
echo date('m-d-Y',strtotime('24-06-2018 next Monday ')); 06-25-2018
06-25-2018 , Here we are getting output as date object and then converting to string by using format. We can directly get date as string by using strtotime. Date as string by using strtotime()
By using this date object we can add, subtract or modify dates

PHP Date Functions Adding Date by using DateInterval
Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



Subscribe to our YouTube Channel here



plus2net.com







hgggfhhfhf ghf

26-08-2014

how to display this (Fri May 14 12:03:24 IST
2010)
slon

23-04-2015

Rather limited class. There is no easy way to get year, month, date etc. Stuid date difference returns a hole class istead of value in items I provide ('d', 'm', 'q' etc) as MSSQL & other conventional DateDiff functions do.




PHP video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer