Hướng dẫn php datetime::modify hours

date_modify

(PHP 5 >= 5.2.0, PHP 7, PHP 8)

DateTime::modify -- date_modifyAlters the timestamp

Description

Object-oriented style

public DateTime::modify(string $modifier): DateTime|false

Return Values

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

Examples

Example #1 DateTime::modify() example

Object-oriented style

$date = new DateTime('2006-12-12');
$date->modify('+1 day');
echo 
$date->format('Y-m-d');
?>

Procedural style

$date date_create('2006-12-12');
date_modify($date'+1 day');
echo 
date_format($date'Y-m-d');
?>

The above examples will output:

Example #2 Beware when adding or subtracting months

$date = new DateTime('2000-12-31');$date->modify('+1 month');
echo 
$date->format('Y-m-d') . "\n";$date->modify('+1 month');
echo 
$date->format('Y-m-d') . "\n";
?>

The above example will output:

See Also

  • strtotime() - Parse about any English textual datetime description into a Unix timestamp
  • DateTimeImmutable::modify() - Creates a new object with modified timestamp
  • DateTime::add() - Modifies a DateTime object, with added amount of days, months, years, hours, minutes and seconds
  • DateTime::sub() - Subtracts an amount of days, months, years, hours, minutes and seconds from a DateTime object
  • DateTime::setDate() - Sets the date
  • DateTime::setISODate() - Sets the ISO date
  • DateTime::setTime() - Sets the time
  • DateTime::setTimestamp() - Sets the date and time based on an Unix timestamp

There are no user contributed notes for this page.

I am trying to add 1 hour to a timestamp field fetched from database using the following code.

Nội dung chính

  • Not the answer you're looking for? Browse other questions tagged php datetime time timestamp datediff or ask your own question.
  • date_modify
  • Description
  • Return Values

date($ls['created_at'], strtotime('+1 hour'));

However, this doesn't seem to work. It returns the same time as in database. Am I missing something? Or, is the code deprecated? What is the proper solution?

asked Sep 12, 2021 at 21:57

1

You need to give it the correct syntax to use this, You need to send the time to change with the change itself in the function - for example (using date for wanted format):

$date = "22-02-2021 14:22:22";
echo date("d-m-Y H:i:s", strtotime($date.' +1 hour'));

This will return:

22-02-2021 15:22:22

Same as this:

echo date("d-m-Y H:i:s", strtotime("22-02-2021 14:22:22 + 1 hour"));

The idea is that you strtotime receives the date and data to change in one string like this :

echo strtotime("22-02-2021 14:22:22 + 2 hour");

Will return:

1614010942

Here I removed the Date Format so I received a unix timestamp format

answered Sep 12, 2021 at 22:07

ShlomtzionShlomtzion

6565 silver badges12 bronze badges

3

Not the answer you're looking for? Browse other questions tagged php datetime time timestamp datediff or ask your own question.

date_modify

(PHP 5 >= 5.2.0, PHP 7, PHP 8)

DateTime::modify -- date_modifyAlters the timestamp

Description

Object-oriented style

public DateTime::modify(string $modifier): DateTime|false

Return Values

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

Examples

Example #1 DateTime::modify() example

Object-oriented style

$date = new DateTime('2006-12-12');
$date->modify('+1 day');
echo 
$date->format('Y-m-d');
?>

Procedural style

$date date_create('2006-12-12');
date_modify($date'+1 day');
echo 
date_format($date'Y-m-d');
?>

The above examples will output:

Example #2 Beware when adding or subtracting months

$date = new DateTime('2000-12-31');$date->modify('+1 month');
echo 
$date->format('Y-m-d') . "\n";$date->modify('+1 month');
echo 
$date->format('Y-m-d') . "\n";
?>

The above example will output:

See Also

  • strtotime() - Parse about any English textual datetime description into a Unix timestamp
  • DateTimeImmutable::modify() - Creates a new object with modified timestamp
  • DateTime::add() - Modifies a DateTime object, with added amount of days, months, years, hours, minutes and seconds
  • DateTime::sub() - Subtracts an amount of days, months, years, hours, minutes and seconds from a DateTime object
  • DateTime::setDate() - Sets the date
  • DateTime::setISODate() - Sets the ISO date
  • DateTime::setTime() - Sets the time
  • DateTime::setTimestamp() - Sets the date and time based on an Unix timestamp

There are no user contributed notes for this page.

How to find days between two dates using PHP datetime?

At PHP 7.1 the DateTime constructor incorporates microseconds when constructed from the current time. Make your comparisons carefully, since two DateTime objects constructed one after another are now more likely to have different values. In the general, I use 'DateTime' to find days between 2 dates.

What is the difference between modify() and DateTime constructor in PHP?

That means that you can't do a Fluid Interface design with it in PHP 5.2. modify () ignores any timezone information in the data while the DateTime constructor does not. does not change the timezone or the time. echo " Timezone '", $dt->getTimezone()->getName() .

What is the use of date_modify () function in PHP?

PHP date_modify() Function 1 Definition and Usage. The date_modify () function modifies the timestamp. 2 Syntax 3 Parameter Values 4 Technical Details. Returns a DateTime object on success.

How do I modify a DateTime object?

Modifies the specified DateTime object, by subtracting the specified DateInterval object. Like DateTimeImmutable::sub () but works with DateTime . The procedural version takes the DateTime object as its first argument. Procedural style only: A DateTime object returned by date_create () . The function modifies this object.