Php set datetime object

DateTime

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');
date_default_timezone_set('America/Chicago'); $date = new DateTime("now"); echo $date->format('d-m-y H:i:s'); echo "

"; $date = new DateTime("tomorrow"); echo $date->format('d-m-y H:i:s');
25-07-23 13:37:33

26-07-23 00:00:00
date_default_timezone_set('America/Chicago'); 
$date = new DateTime('2012-04-15 22:15:40');
echo $date->format('Y-m-d H:i:s') ;
$date = date_create('2018-11-29 23:15:40'); echo date_format($date, 'Y/m/d H:i:s');

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

echo date(DateTime::ISO8601); echo "
"; $dt_object = new DateTime('NOW'); echo $dt_object->format('c'); echo "
"; echo $dt_object->format(DateTime::ISO8601);
2023-07-25T13:37:33-0500
2023-07-25T13:37:33-05:00
2023-07-25T13:37:33-0500
Code Output
$d = new DateTime(‘now’);
echo $d->format(‘Y-m-d’) ;
2023-07-25
$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’) ;
2023-07-26
$d = new DateTime(‘Yesterday’);
echo $d->format(‘Y-m-d’) ;
2023-07-24
$d = new DateTime(‘+1 day’);
echo $d->format(‘Y-m-d’) ;
2023-07-26
$d = new DateTime(‘+1 week’);
echo $d->format(‘Y-m-d’) ;
2023-08-01
$d = new DateTime(‘+1 week 5 days’);
echo $d->format(‘Y-m-d’) ;
2023-08-06
$d = new DateTime(‘+1 month’);
echo $d->format(‘Y-m-d’) ;
2023-08-25
$d = new DateTime(‘+1 month 10 days’);
echo $d->format(‘Y-m-d’) ;
2023-09-04
$d = new DateTime(‘+3 months -1 day’);
echo $d->format(‘Y-m-d’) ;
2023-10-24
$d = new DateTime(‘+3 years’);
echo $d->format(‘Y-m-d’) ;
2026-07-25
$d = new DateTime(‘+1 year 5 days’);
echo $d->format(‘Y-m-d’) ;
2024-07-30
$d = new DateTime(‘Monday’);
echo $d->format(‘Y-m-d’) ;
2023-07-31
$d = new DateTime(‘Monday +1 day ‘);
echo $d->format(‘Y-m-d’) ;
2023-08-01
$d = new DateTime(‘first day of this month’);
echo $d->format(‘Y-m-d’) ;
2023-07-01
$d = new DateTime(‘last day of this month’);
echo $d->format(‘Y-m-d’) ;
2023-07-31
$d = new DateTime(‘first day of previous month’);
echo $d->format(‘Y-m-d’) ;
2023-06-01
$d = new DateTime(‘last day of previous month’);
echo $d->format(‘Y-m-d’) ;
2023-06-30
$d = new DateTime(‘first day of next month’);
echo $d->format(‘Y-m-d’) ;
2023-08-01
$d = new DateTime(‘last day of next month’);
echo $d->format(‘Y-m-d’) ;
2023-08-31
$d = new DateTime(‘last day of next month +1 hour’);
echo $d->format(‘Y-m-d : H:i:s’) ;
2023-08-31 : 14:37:33
$d = new DateTime(‘Monday +1 hour +20 minutes +10 seconds’);
echo $d->format(‘Y-m-d : H:i:s’) ;
2023-07-31 : 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

plus2net.com

Источник

PHP DateTime

Summary: in this tutorial, you’ll learn how to work with the date and time in an object-oriented way.

Читайте также:  Ordered numbered list html

Introduction to the PHP DateTime class

PHP provides a set of date and time classes that allow you to work with the date and time in an object-oriented way.

To create a new date and time object, you use the DateTime class. For example:

 $datetime = new DateTime(); var_dump($datetime);Code language: PHP (php)
object(DateTime)#1 (3) ["date"]=> string(26) "2021-07-15 06:30:40.294788" ["timezone_type"]=> int(3) ["timezone"]=> string(13) "Europe/Berlin" >Code language: PHP (php)

The DateTime object represents the current date and time in the timezone specified in the PHP configuration file ( php.ini )

To set a new timezone, you create a new DateTimeZone object and pass it to the setTimezone() method of the DateTime object:

 $datetime = new DateTime(); $timezone = new DateTimeZone('America/Los_Angeles'); $datetime->setTimezone($timezone); var_dump($datetime);Code language: PHP (php)
object(DateTime)#1 (3) ["date"]=> string(26) "2021-07-14 21:33:27.986925" ["timezone_type"]=> int(3) ["timezone"]=> string(19) "America/Los_Angeles" >Code language: PHP (php)

In this example, we create a new DateTimeZone object and set it to «America/Los_Angeles» . To get valid timezones supported by PHP, check out the timezone list.

To format a DateTime object, you use the format() method. The format string parameters are the same as those you use for the date() function. For example:

 $datetime = new DateTime(); echo $datetime->format('m/d/Y g:i A'); Code language: PHP (php)
07/15/2021 6:38 AMCode language: PHP (php)

To set a specific date and time, you can pass a date & time string to the DateTime() constructor like this:

 $datetime = new DateTime('12/31/2019 12:00 PM'); echo $datetime->format('m/d/Y g:i A');Code language: PHP (php)

Or you can use the setDate() function to set a date:

 $datetime = new DateTime(); $datetime->setDate(2020, 5, 1); echo $datetime->format('m/d/Y g:i A');Code language: PHP (php)
05/01/2020 6:42 AMCode language: PHP (php)

The time is derived from the current time. To set the time, you use the setTime() function:

 $datetime = new DateTime(); $datetime->setDate(2020, 5, 1); $datetime->setTime(5, 30, 0); echo $datetime->format('m/d/Y g:i A');Code language: PHP (php)
05/01/2020 5:30 AMCode language: PHP (php)

Since the setDate() , setTime() , and setTimeZone() method returns the DateTime object, you can chain them like this which is quite convenient.

 $datetime = new DateTime(); echo $datetime->setDate(2020, 5, 1) ->setTime(5, 30) ->setTimezone(new DateTimeZone('America/New_York')) ->format('m/d/Y g:i A');Code language: PHP (php)
04/30/2020 11:30 PMCode language: PHP (php)

Creating a DateTime object from a string

When you pass the date string ’06/08/2021′ to the DateTime() constructor or setDate() function, PHP interprets it as m/d/Y . For example:

 $datetime = new DateTime('06/08/2021'); echo $datetime->format('F jS, Y'); Code language: PHP (php)
June 8th, 2021Code language: PHP (php)

If you want to pass it as August 6th, 2021, you need to use the – or . instead of /. For example:

 $datetime = new DateTime('06-08-2021'); echo $datetime->format('F jS, Y');Code language: PHP (php)
August 6th, 2021Code language: PHP (php)

However, if you want to parse the date string ’06/08/2021′ as d/m/Y, you need to replace the / with – or . manually:

 $ds = '06/08/2021'; $datetime = new DateTime(str_replace('/', '-', $ds)); echo $datetime->format('F jS, Y');Code language: PHP (php)
August 6th, 2021Code language: PHP (php)

A better way to do it is to use the createFromFormat() static method of the DateTime object:

 $ds = '06/08/2021'; $datetime = DateTime::createFromFormat('d/m/Y', $ds); echo $datetime->format('F jS, Y');Code language: PHP (php)

In this example, we pass the date format as the first argument and the date string as the second argument.

Note that when you pass a date string without the time, the DateTime() constructor uses midnight time. However, the createFromFormat() method uses the current time.

Comparing two DateTime objects

PHP allows you to compare two DateTime objects using the comparison operators including >, >=, . For example:

 $datetime1 = new DateTime('01/01/2021 10:00 AM'); $datetime2 = new DateTime('01/01/2021 09:00 AM'); var_dump($datetime1 < $datetime2); // false var_dump($datetime1 > $datetime2); // true var_dump($datetime1 == $datetime2); // false var_dump($datetime1 $datetime2); // 1Code language: PHP (php)

Calculating the differences between two DateTime objects

The diff() method of the DateTime() object returns the difference between two DateTime() objects as a DateInterval object. For example:

 $dob = new DateTime('01/01/1990'); $to_date = new DateTime('07/15/2021'); $age = $to_date->diff($dob); var_dump($age);Code language: PHP (php)
object(DateInterval)#3 (16) ["y"]=> int(31) ["m"]=> int(6) ["d"]=> int(14) ["h"]=> int(0) ["i"]=> int(0) ["s"]=> int(0) ["f"]=> float(0) ["weekday"]=> int(0) ["weekday_behavior"]=> int(0) ["first_last_day_of"]=> int(0) ["invert"]=> int(1) ["days"]=> int(11518) ["special_type"]=> int(0) ["special_amount"]=> int(0) ["have_weekday_relative"]=> int(0) ["have_special_relative"]=> int(0) >Code language: PHP (php)

The DateInterval represents the differences between two dates in the year, month, day, hour, etc. To format the difference, you use the DateInterval ‘s format. For example:

 $dob = new DateTime('01/01/1990'); $to_date = new DateTime('07/15/2021'); echo $to_date->diff($dob)->format('%Y years, %m months, %d days');Code language: PHP (php)
31 years, 6 months, 14 daysCode language: PHP (php)

Adding an interval to a DateTime object

To add an interval to date, you create a new DateInterval object and pass it to the add() method. The following example adds 1 year 2 months to the date 01/01/2021 :

 $datetime = new DateTime('01/01/2021'); $datetime->add(new DateInterval('P1Y2M')); echo $datetime->format('m/d/Y');Code language: PHP (php)
03/01/2022Code language: PHP (php)

To format a date interval, you use the date interval format strings.

To subtract an interval from a DateTime object, you create a negative interval and use the add() method.

Summary

  • Use the DateTime class to work with the date and time.
  • Use the DateTimeZone class to work with time zones.
  • Use the comparison operators to compare two DateTime objects.
  • Use the diff() method to calculate the difference between to DateTime objects.
  • Use the DateInterval class to represent a date and time interval.
  • Use the add() method to add an interval to or subtract an interval from a DateTime object.

Источник

DateTime::setTime

Resets the current time of the DateTime object to a different time.

Parameters

Procedural style only: A DateTime object returned by date_create(). The function modifies this object.

Return Values

Returns the DateTime object for method chaining or false on failure.

Changelog

Examples

Example #1 DateTime::setTime() example

 $date = new DateTime('2001-01-01'); $date->setTime(14, 55); echo $date->format('Y-m-d H:i:s') . "\n"; $date->setTime(14, 55, 24); echo $date->format('Y-m-d H:i:s') . "\n"; ?>
 $date = date_create('2001-01-01'); date_time_set($date, 14, 55); echo date_format($date, 'Y-m-d H:i:s') . "\n"; date_time_set($date, 14, 55, 24); echo date_format($date, 'Y-m-d H:i:s') . "\n"; ?>

The above examples will output something similar to:

2001-01-01 14:55:00 2001-01-01 14:55:24 

Example #2 Values exceeding ranges are added to their parent values

 $date = new DateTime('2001-01-01'); $date->setTime(14, 55, 24); echo $date->format('Y-m-d H:i:s') . "\n"; $date->setTime(14, 55, 65); echo $date->format('Y-m-d H:i:s') . "\n"; $date->setTime(14, 65, 24); echo $date->format('Y-m-d H:i:s') . "\n"; $date->setTime(25, 55, 24); echo $date->format('Y-m-d H:i:s') . "\n"; ?>

The above example will output:

2001-01-01 14:55:24 2001-01-01 14:56:05 2001-01-01 15:05:24 2001-01-02 01:55:24 

See Also

PHP 8.2

(PHP 5 5.2.0, 7, 8) DateTime::setDate date_date_set Sets the Object-oriented style Procedural style Resets the current date of DateTime object to different

(PHP 5 5.2.0, 7, 8) DateTime::setISODate date_isodate_set Sets the Object-oriented style Procedural style Set a date according to the ISO 8601 standard

(PHP 5 5.3.0, 7, 8) DateTime::setTimestamp date_timestamp_set Sets the and based on Unix Object-oriented style Procedural style Sets the date and time

(PHP 5 5.2.0, 7, 8) DateTime::setTimezone date_timezone_set Sets the for object Object-oriented style Procedural style Sets a new timezone for DateTime

Источник

Оцените статью