Add time to a date time using php - php

I have 3 fields in database startdate(datetime),global_time(time),enddate(datetime).Start date will be today datetime.
$start_date = date('Y-m-d h:i:s');
$global_time = '00:45:30' // comes from database global_time
Now i want to sum $start_date and $global_time and store into database field enddate.Can anyone suggest me to how to do this?

$start_date = new DateTime('2017-01-22 05:05:00');
$global_time = "00:45:30";
sscanf($global_time, "%d:%d:%d", $hours, $minutes, $seconds);
$time_seconds = isset($seconds) ? $hours * 3600 + $minutes * 60 + $seconds : $hours * 60 + $minutes;//Converted in to seconds
$newtime="PT".$time_seconds."S";
$start_date->add(new DateInterval($newtime));
$enddate = $start_date->format('Y-m-d H:i:s');
echo $enddate;
PT0H45M30S for a interval of 0 hour, 45 minutes and 30 seconds.

Related

PHP - Difference between Time in Minutes

I'm trying to calculate the difference of two times. I'm using this method to return the difference in minutes.
$datetime1 = new DateTime('9.00am');
$datetime2 = new DateTime('10.15am');
$interval = $datetime1->diff($datetime2);
$timeDuration = $interval->format('%im');
However, the $timeDuration returns 15m instead of 75m.
Any help that could correct the code to let it return the exact correct difference in minutes ?
Because the difference between 9am and 10:15am is 1 hour ($interval->h) and 15 minutes ($interval->i), not 75 minutes literally.
If you wish to get the total number of minutes, you have to calculate it:
$differenceInMinutes = $interval->i + ($interval->h * 60);
If you wish to have more universal solution, I would go for the unix time:
$differenceInSeconds = abs($datetime1->format('U') - $datetime2->format('U'));
$differenceInMinutes = ceil($differenceInSeconds / 60);
The simple way to get different in minutes
<?php
$start = date_create('1990-01-01 09:00:00');
$end = date_create('1990-01-01 10:15:00');
$interval=date_diff($end, $start);
$hours = $interval->format('%h');
$minutes = $interval->format('%i');
$in_minutes = $hours * 60 + $minutes;
echo 'Diff. in minutes is: '.$in_minutes;
?>
When you are trying to calculate the difference between two DateTime's like $datetime1 = new DateTime('9.00am'); and $datetime2 = new DateTime('10.15am'); you know that the difference is 75 minutes, but the resulto of diff() method is 1 hour for $interval->h and 15 minutes for $interval->i. So this is how you can calculate it:
$datetime1 = new DateTime('9.00am');
$datetime2 = new DateTime('10.15am');
$interval = $datetime1->diff($datetime2);
$hours = $interval->format('%h');
$minutes = $interval->format('%i');
$difference_in_minutes = $hours * 60 + $minutes;
echo 'Difference in minutes is: ' . $difference_in_minutes . "\n";
There a working snipped in this link

Adding a time string HH:MM (hours and minutes) to date timestamp

I need to add a time stored in strong (format: hh:mm) to a date timestamp which before have no hours information (e.g. 2018-06-12 00:00:00). How to do it?
I would like to do this:
$startTime = "11:45";
$startDate = 1528754400; // 2018-06-12 00:00:00
$startDate = $startDate + $startTime; //2018-06-12 11:45:00
You can do some simple math and get there. Multiple minutes by 60 and hours by 3600 and you'll get the number of seconds the hours/minutes take up.
$startTime = "11:45";
$startDate = 1528754400;
$time = explode(':', $startTime);
$minutes = $time[1] * 60;
$hours = $time[0] * 3600;
Then just piece it back together. (the timestamp is the number of seconds since 1970 so just adding to it should be fine)
$startDate + $minutes + $hours
https://3v4l.org/U2jHL
An alternative approach would be using the datetime class.
$startTime = "11:45";
list($hours, $minutes) = explode(':', $startTime);
$date = new DateTime(date('Y-m-d', 1528754400));
$date->add(new DateInterval('PT' . $hours .'H' . $minutes . 'M'));
https://3v4l.org/D51XB
Just came across with this using strtotime:
$startTime = "11:45";
$startDate = 1528754400; // 2018-06-12 00:00:00
$startTimeTmp = explode(":",$startTime);
$finalTimestamp = strtotime("+".$startTimeTmp[0]." hours ".$startTimeTmp[1]." minutes", $startDate);

Add fractional hours to a DateTime in php

I have a system which I need to add a certain amount of fractional hours.
I've been searching and this is what I got, by far it's the most accurate method, but still doesn't give me the answer I need
function calculateHours($hours){
$now = new DateTime("2017-10-25 10:23:00");
$time = array();
$time = explode(".", $hours);
$time [1] += $time [0]*60;
$now->modify("+".$time[1]." hours");
return $now;
}
$diff = 119.23;
$answer = calculateHours($diff);
echo $answer ->format('Y-m-d H:i:s');
The answer that I want to reach is "2017-11-09 11:00:00" and I receive "2017-10-25 12:22:23" instead
Adding the hours is not correct. When you multiply hours times 60 it will make minutes.
This code should work.
function calculateHours($hours){
$now = new DateTime("2017-10-25 10:23:00");
$time = explode(".", $hours);
$time[1] += $time[0]*60;
$now->modify("+".$time[1]." minutes");
return $now;
}
$diff = 119.23;
$answer = calculateHours($diff);
echo $answer->format('Y-m-d H:i:s');
Result is 2017-10-30 09:46:00
You should use DateInterval php class to create an inverval with x seconds from your $hours variable.
Then you just have to use the datetime add interval method to modify your date
Please take a look a this example
function calculateHours($hours){
$now = new DateTime("2017-10-25 10:23:00");
var_dump($now);
$timeParts = explode(".", $hours);
// Where 23 is a percentage of on hour
$minutes = $timeParts[0] * 60 + round($time[1] * 60 / 100);
// Where 23 is the number of minutes
$minutes = $timeParts[0] * 60 + $time[1];
$interval = new DateInterval(sprintf('PT%dM', $minutes));
$now->add($interval);
echo $now->format('Y-m-d H:i:s');
return $now;
}
Use date_add
date_add($now, date_interval_create_from_date_string($tempo[1]' hours'));
or as object:
$now->add( DateInterval::createFromDateString($tempo[1].' hours'));

PHP: Days, hours and minutes left to a certain date?

I'm trying to get remaining Days, hours and minutes to a certain date using php.
However i get a very strange output from my code which looks like this:
-16828 days and -11 hours and -21 minutes and -24 seconds
The future dates are stored in the mysql database in this format:
29/01/2016 7pm
So I went ahead and done this:
$Draw_time = "29/01/2016 7pm";
$date = $Draw_time;
$timestamp = strtotime($date);
$new_date = date('Y-m-d a',$timestamp );
$seconds = strtotime($new_date) - time();
$days = floor($seconds / 86400);
$seconds %= 86400;
$hours = floor($seconds / 3600);
$seconds %= 3600;
$minutes = floor($seconds / 60);
$seconds %= 60;
echo "$days days and $hours hours and $minutes minutes and $seconds seconds";
But when i run this code, I get the above strange output!
I understand that this could be because of a number reasons but the only thing i could think of is the fact that I am using a in my format?
Could someone please advise on this issue?
Simply use DateTime class like as
$Draw_time = "29/01/2016 7pm";
$date = DateTime::createFromFormat("d/m/Y ha",$Draw_time);
$date2 = new DateTime();
echo $diff = $date2->diff($date)->format("%a days and %H hours and %i minutes and %s seconds");
Try this
<?php
$Draw_time = str_replace('/', '-', "29/01/2016 7pm");
$now = new DateTime();
$futureDate = new DateTime($Draw_time);
$interval = $futureDate->diff($now);
echo $interval->format("%a days %h hours %i minutes %s seconds");
?>
Try this.
$draw_time = "2016/01/29 7pm";
$date_time = explode(" ", $draw_time);// make separate date and time in array
$date = strtotime($date_time[0]); // convert your date(2016/01/29) into php time
$time = strtotime($date_time[1]); // convert your time(7pm) into php time
$date = $date + $time; // make total time to count
$new_Date = $date - (time()); // convert into difference from current time
$day = $new_Date % 86400;
$hrs = $new_Date % 3600;
$min = $new_Date % 60;
echo "Day= ".(date("d",$day));
echo " Hours= ".(date("h",$hrs));
echo " Minutes= ".(date("i",$min));

How many seconds from mysql time format to now

I use this function to set a time date("Y-m-d H:i:s", strtotime("+2 minutes"). Now I want to compare that value with the current time to find the amount of seconds it's left.
For example compare: $next_action = 2011-01-16 18:03:00 and $now = 2011-01-16 18:01:23. To find the amount of seconds.
strtotime can convert mysql timestamps to unix timestamps. so you just convert both of them to UNIX timestamps and subtract one from other, and you'll get the difference in seconds.
$next_action = "2011-01-16 18:03:00";
$now = "2011-01-16 18:01:23";
echo strtotime($next_action)-strtotime($now);
Why did you convert them to "Y-m-d H:i:s" in the first place? Unix timestamps are much easier to work with.
$start_time = date("Y-m-d H:i:s", strtotime("+2 minutes"));
$time_diff = (time() - strtotime($start_time)); // difference in seconds
$seconds = $time_diff % 60;
$minutes = ($time_diff - $seconds) % (60 * 60);
$hours = ($time_diff - ($minutes * 60) - $seconds) / (24 * 60 * 60);
Untested, but it would probably go something like this.

Categories