date_modify
[PHP 5 >= 5.2.0, PHP 7, PHP 8]
DateTime::modify -- date_modify — Alters 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
Procedural style
The above examples will output:
Example #2 Beware when adding or subtracting months
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_modify — Alters 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
Procedural style
The above examples will output:
Example #2 Beware when adding or subtracting months
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.