DateTime
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 output2015-02-02 03:02:50
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
02-02-15 03:02:50
03-02-15 00:00:00
Another way
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 here2012-04-15 22:15:40
By using this date object we can add, subtrack or modify datesScript | Description |
---|---|
Date Interval | Creating date interval object using standard format |
Adding date | Adding date and time to the date object |
Date difference | Difference in days month etc between two date object |
Modifying date | Change the date object by adding or subtracting days, months, time etc |
Set Date | Changing the date object by adding new date parameters |
Set Time | Changing time part of the date object |
TimeZone Get | Getting timezone set at the server |
TimeZone Set | Setting a new timezone of the server |
TimeZone List | List of timezone can be used or available |
gettimeofday | Time elapsed between current time and Epoch time |
localtime | Date , time, year etc as an array |
0 comments:
Post a Comment