date_timezone_set
(PHP 5 >= 5.2.0, PHP 7, PHP 8)
DateTime::setTimezone -- date_timezone_set — Sets the time zone for the DateTime object
Parameters
objectProcedural style only: A DateTime object returned by date_create(). The function modifies this object.
timezoneA DateTimeZone object representing the desired time zone.
Return Values
Returns the DateTime object for method chaining. The underlaying point-in-time is not changed when calling this method.
Examples
Example #1 DateTime::setTimeZone() example
Object-oriented style
<?php
$date = new DateTime('2000-01-01', new DateTimeZone('Pacific/Nauru'));
echo $date->format('Y-m-d H:i:sP') . "\n";$date->setTimezone(new DateTimeZone('Pacific/Chatham'));
echo $date->format('Y-m-d H:i:sP') . "\n";
?>
Procedural style
<?php
$date = date_create('2000-01-01', timezone_open('Pacific/Nauru'));
echo date_format($date, 'Y-m-d H:i:sP') . "\n";date_timezone_set($date, timezone_open('Pacific/Chatham'));
echo date_format($date, 'Y-m-d H:i:sP') . "\n";
?>
The above examples will output:
2000-01-01 00:00:00+12:00 2000-01-01 01:45:00+13:45
See Also
- DateTimeImmutable::setTimezone() - Sets the time zone
- DateTime::getTimezone() - Return time zone relative to given DateTime
- DateTimeZone::__construct() - Creates new DateTimeZone object
There are no user contributed notes for this page.
date_time_set
(PHP 5 >= 5.2.0, PHP 7, PHP 8)
DateTime::setTime -- date_time_set — Sets the time
Description
Object-oriented style
public DateTime::setTime(
int $hour,
int
$minute,
int $second = 0,
int $microsecond = 0
): DateTime
Like DateTimeImmutable::setTime() but works with DateTime.
The procedural version takes the DateTime object as its first argument.
Parameters
objectProcedural style only: A DateTime object returned by date_create(). The function modifies this object.
hourHour of the time.
minuteMinute of the time.
secondSecond of the time.
microsecondMicrosecond of the time.
Return Values
Returns the modified DateTime object for method chaining or false on failure.
Changelog
8.1.0 | The behaviour with double existing hours (during the fall-back DST transition) changed. Previously PHP would pick the second occurrence (after the DST transition), instead of the first occurrence (before DST transition). |
7.1.0 | The microsecond parameter was added. |
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
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:22Same 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:
1614010942Here 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
<?php
$date = new DateTime('2006-12-12');
$date->modify('+1 day');
echo $date->format('Y-m-d');
?>
Procedural style
<?php
$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
<?php
$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.