Add hours in a date in PHP [duplicate] - php

This question already has answers here:
Time calculation in php (add 10 hours)?
(7 answers)
Closed 9 years ago.
Want to add hours in a date .
Please help.
The PHP code is:
$createdDate=date_create("2013-03-17 07:11:00");
$hour = 4;
I tried using strtotime. But it gives an error.
Thanks

Use DateTime modify method.
$date = new DateTime("2013-03-17 07:11:00");
$date->modify("+4 hours");
echo $date->format("Y-m-d H:i:s");
DEMO.

You could use DateInterval:
$iv = new DateInterval("PT{$hour}H");
$createdDate->add($iv);
// $createdDate is now modified

You can use date_modify:
date_modify($createdDate, "+4 hours");

Related

Extract hour information from a date string [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 7 years ago.
Is it possible extract hour value from a date string like below
20151211091805 +0000
expected output-
09:18:05 (this should also be string)
Try this:
$t = "20151211091805";
echo date('H:i:s',strtotime($t));
Use PHP's date function.
echo date("H:i:s", strtotime("20151211091805"));
Another way using DateTime class.
$dateTime = new DateTime("20151211091805");
echo $dateTime->format('H:i:s');
Try this..
<?php
$timestamp = 20151211091805;
$date = new DateTime("$ts");
echo date_format($date, 'H:i:s');
?>

Convert string date to new format [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 8 years ago.
How to convert a string, for instance
$datestring = '28.08.14 10.42';
To a new format like date("YmdHis.u")
Thank you
Use date function. There is no such format as what you have required but the what I think you want to achieve is this.
DEMO
<?php
$datestring = '28.08.14 10.42';
$date = date("Y-m-d H:i:s:u", strtotime($datestring));
echo $date; //outputs 2014-08-28 10:42:00:000000
?>
Try this
$datestring = '28.08.14 10.42';
echo date("YmdHis.u",strtotime($datestring));

Adding different times in php [duplicate]

This question already has answers here:
Adding 30 minutes to time formatted as H:i in PHP
(7 answers)
Closed 9 years ago.
Hello just a simple question here.
Php's time related functions are confusing me ;-(
Given 2 variables
$start = "2013-07-25 20:24:13" ('Y-m-d H:i:s');
$duration = "0:55" ('H:i');
How may I add $duration to $start? should result in:
"2013-07-25 21:19:13"
Use DateTime() with DatePeriod()
$dt = new DateTime('2013-07-25 20:24:13');
$dt->add(new DatePeriod('P55M'));
echo $dt->format('Y-m-d H:i:s');

Converting month name in given date into month number [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP date format converting
I have a date like 2012-september-09. How to convert this into 2012-09-09 using PHP?
You can try with strptime
$date = '2012-september-09';
$strp = strptime($date, '%Y-%B-%d');
$ymd = sprintf('%04d-%02d-%02d', $strp['tm_year'] + 1900, $strp['tm_mon'] + 1, $strp['tm_mday']);
$new_date = new DateTime($ymd);
echo $new_date->format('Y-m-d');
here's a Codepad
Try this
Change
<?php echo date('Y-m-d',strtotime('2012-september-09'));?>
To
<?php echo date('Y-m-d',strtotime('09-september-2012'));?>

How to change date format from DD/MM/YYYY to YYYY-MM-DD? [duplicate]

This question already has answers here:
How to reformat date in PHP?
(9 answers)
Closed 6 years ago.
How to change format of date string using PHP?
From: 06/16/2010
To: 2010-06-16
$date = "06/16/2010";
echo date('Y-m-d', strtotime($date)); // outputs 2010-06-16
Using the strtotime function.
You should use \DateTime and get rid of strings as soon as possible:
$date = DateTime::createFromFormat('m/d/Y', '06/16/2010'); // \DateTime object
echo $date->format('Y-m-d'); // 2010-06-16
See more:
http://php.net/manual/en/datetime.createfromformat.php
php -r 'echo date("Y-m-d", strtotime("06/16/2010"));'

Categories