Adding different times in php [duplicate] - php

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');

Related

DateTime comparison not working PHP [duplicate]

This question already has answers here:
How to compare two time in PHP
(11 answers)
Closed 6 years ago.
timing comparison not working out if passed dynamically
$date = new DateTime();
$date->setTimezone(new DateTimeZone('America/New_York'));
$myTime =$date->format('H:i');
$myDay =$date->format('l');
if(($s<=$myTime) && ($e>=$myTime||$e=="00:00"))
{$open =1;}
Following works if hard coded
if(("11:30"<="05:39")&&("23:00">="05:39"||"23:00"=="00:00"))
{$open =1;}
where m i going wrong
// your first date
$dateA = '2008-03-01 13:34';
// your second date
$dateB = '2007-04-14 15:23';
if(strtotime($dateA) > strtotime($dateB)){
// something here
}
first of all you are using strings to compare time. Convert to timesstamp using strtotime (http://php.net/manual/en/function.strtotime.php) or mktime(http://php.net/manual/en/function.mktime.php) or use DateTime class (http://php.net/manual/en/class.datetime.php). When comparing strings they are not compared as time, but as character sequences.

I want result in hours between two datetime [duplicate]

This question already has answers here:
PHP: producing relative date/time from timestamps
(10 answers)
How do I compare two DateTime objects in PHP 5.2.8?
(8 answers)
Closed 7 years ago.
Here, I want to get hours between two dates.
If I subtract time between two dates, I want 23 hours in a result.
My field's datatype is as datetime.
My code is like,
$usr_check_login = $this -> applib->get_any_field('users_attendance',array('user_id'=>$this->session->userdata('user_id'),'clock_out'=>'0000-00-00 00:00:00'),'clock_in');
$cur_time = date('d-m-Y H:i:s');
$clk_time = date('d-m-Y H:i:s',strtotime($usr_check_login));
$res = $cur_time - $clk_time;
$hours = $res / ( 60 * 60 );
echo $hours."<br>";
echo $cur_time."</br>";
echo $clk_time."</br>";
I am getting result like,
0.00027777777777778
23-01-2016 11:29:19
22-01-2016 11:02:39
Here I want result as 24 hours as the dates are changed.
What can be the solution?

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');
?>

find difference in seconds [duplicate]

This question already has an answer here:
Difference between 2 dates in seconds [duplicate]
(1 answer)
Closed 8 years ago.
date_default_timezone_set('America/New_York');
$search_date = '2012-12-19 13:22:00';
$right_now = date('Y-m-d H:i:s');
$search_date = new DateTime($search_date);
$right_now = new DateTime($right_now);
$interval = $search_date->diff($right_now);
echo $interval->format('%R%s seconds');
This displays how many seconds are different between the search date and right now.
I would expect it to return more than a two digit value because there is more than a 99 second difference between the two dates, so I am not sure what I am doing wrong.
Alternatively, with very little change to your original code:-
date_default_timezone_set('America/New_York');
$search_date = new DateTime('2012-12-19 13:22:00');
$right_now = new DateTime();
$seconds = $right_now->getTimestamp() - $search_date->getTimestamp();

Add hours in a date in PHP [duplicate]

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");

Categories