DateTime in PHP class is available in PHP 5 and higher versions . This creates a date object which we can use in our script. Here is the code to create a date object and use it .
$today   = new DateTime;
echo $today->format('Y-m-d H:i:s');
The above example will display today's date and time in Year-month-date Hour:minutes:second format. Here is the output
2018-01-16 01:54:39
If you are getting warning message saying not to rely on system time zone , then add this line to the top.
$today = new DateTime;
echo $today->format('Y-m-d H:i:s');
Let us try differently
$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
16-01-18 01:54:39

17-01-18 00:00:00
Another way
$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

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
By using this date object we can add, subtrack or modify dates
Script Description
createfromformat()Change date format
Date IntervalCreating date interval object using standard format
Adding dateAdding date and time to the date object
Date differenceDifference in days month etc between two date object
Modifying dateChange the date object by adding or subtracting days, months, time etc
Set DateChanging the date object by adding new date parameters
Set TimeChanging time part of the date object
TimeZone GetGetting timezone set at the server
TimeZone SetSetting a new timezone of the server
TimeZone ListList of timezone can be used or available
gettimeofdayTime elapsed between current time and Epoch time
localtime Date , time, year etc as an array

Subscribe to our mailing list

* indicates required
Subscribe to plus2net
Visitors Rating
Your Rating


More On Date Object
hgggfhhfhf ghf


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


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.

Post Comment This is for short comments only. Use the forum for more discussions.

Subscribe to our mailing list

* indicates required
Subscribe to plus2net
HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us
©2000-2018 All rights reserved worldwide Privacy Policy Disclaimer