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

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

Related

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

Add time to a date time using 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.

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 to Subtract Minutes

I want to send a reminder email.I don't want to use cron on Linux/Unix/BSD box or Scheduled Tasks on Windows.
I'm trying to subtract 15 minutes from the current time.
here is my code so far (doesn't work):
$days = date("j",time());
$months = date("n",time());
$years = date("Y",time());
$hours = date("G",time());
$mins = (date("i",time()));
$secs = date("s",time());
$mins = $mins-15;
To subtract 15 minutes from the current time, you can use strtotime():
$newTime = strtotime('-15 minutes');
echo date('Y-m-d H:i:s', $newTime);
Change the date into a timestamp (in seconds) then minus 15 minutes (in seconds) and then convert back to a date:
$date = date("Y-m-d H:i:s");
$time = strtotime($date);
$time = $time - (15 * 60);
$date = date("Y-m-d H:i:s", $time);
You can use DateInterval
$date = new DateTime();
$interval = new DateInterval("PT15M");
$interval->invert = 1;
$date->add($interval);
echo $date->format("c") . "\n";
you can try this as well,
$dateTimeMinutesAgo = new DateTime("15 minutes ago");
$dateTimeMinutesAgo = $dateTimeMinutesAgo->format("Y-m-d H:i:s");
How about substracting the 15 minutes from time() before converting it?
$time = time() - (15 * 60);
And then use $time instead of time() in your code.
$currentTime = date('Y-m-d H:i:s');
$before15mins = strtotime('-15 minutes');
echo date('Y-m-d H:i:s', $before15mins);
You can also use strtotime function to subtract days, hours and/or seconds from current time.
echo date('Y-m-d H:i:s', strtotime('-15 minutes'));
Following is the way you can add days / hours / minutes / sec to current time
$addInterval = date('Y-m-d H:i:s', strtotime("+$days days $hours hours $minute minute $sec second", strtotime(currentTime)));
You can also use DateInterval object
<?php
$date = new DateTime('Y-m-d H:i:s');
$date->sub(new DateInterval('PT10H30S'));
echo $date->format('Y-m-d H:i:s');?>
Try using
$min = time() - 900; //900 seconds = 15 minutes
To subtract 15 minutes you can do:
date('Y-m-d H:i:s', (time() - 60 * 15));
You can replace 15 with the number of minutes you want.
In case you're looking to subtract seconds you can simply do:
date('Y-m-d H:i:s', (time() - 10));
In this way you'll subtract 10 seconds.
If you have only time value than below will be useful
// Your time
$time = '12:15:00';
// Returned value '12:00:00'
$newTime = date('H:i:s', strtotime($time) - (15*60));
I know this question is outdated but i just want to share how i did it in easy way
$current = new DateTime("10 minutes ago", new DateTimeZone('Asia/Manila') );
echo $current->format("Y-m-d H:i:s");
//To Get Current DateTime
$currentDate = date("Y-m-d H:i:s");
//To Get Current DateTime - 15Min
$oldDate = date("Y-m-d H:i:s", strtotime($currentDate) - (15 * 60));
echo $currentDate;
echo $oldDate;

How do I find the hour difference between two dates in PHP?

I have two dates, formated like "Y-m-d H:i:s". I need to compare these two dates and figure out the hour difference.
You can convert them to timestamps and go from there:
$hourdiff = round((strtotime($time1) - strtotime($time2))/3600, 1);
Dividing by 3600 because there are 3600 seconds in one hour and using round() to avoid having a lot of decimal places.
You can use DateTime class also -
$d1= new DateTime("06-08-2015 01:33:26pm"); // first date
$d2= new DateTime("06-07-2015 10:33:26am"); // second date
$interval= $d1->diff($d2); // get difference between two dates
echo ($interval->days * 24) + $interval->h; // convert days to hours and add hours from difference
As an addition to accepted answer I would like to remind that \DateTime::diff is available!
$f = 'Y-m-d H:i:s';
$d1 = \DateTime::createFromFormat($date1, $f);
$d2 = \DateTime::createFromFormat($date2, $f);
/**
* #var \DateInterval $diff
*/
$diff = $d2->diff($d1);
$hours = $diff->h + ($diff->days * 24); // + ($diff->m > 30 ? 1 : 0) to be more precise
\DateInterval documentation.
$seconds = strtotime($date2) - strtotime($date1);
$hours = $seconds / 60 / 60;
You can try this :
$time1 = new DateTime('06:56:58');
$time2 = new DateTime('15:35:00');
$time_diff = $time1->diff($time2);
echo $time_diff->h.' hours';
echo $time_diff->i.' minutes';
echo $time_diff->s.' seconds';
Output:
8 hours 38 minutes 2 seconds
The problem is that using these values the result is 167 and it should be 168:
$date1 = "2014-03-07 05:49:23";
$date2 = "2014-03-14 05:49:23";
$seconds = strtotime($date2) - strtotime($date1);
$hours = $seconds / 60 / 60;
$date1 = date_create('2016-12-12 09:00:00');
$date2 = date_create('2016-12-12 11:00:00');
$diff = date_diff($date1,$date2);
$hour = $diff->h;
This is because of day time saving.
Daylight Saving Time (United States) 2014 began at 2:00 AM on
Sunday, March 9.
You lose one hour during the period from $date1 = "2014-03-07 05:49:23" to
$date2 = "2014-03-14 05:49:23";
You can try this:
$dayinpass = "2016-09-23 20:09:12";
$today = time();
$dayinpass= strtotime($dayinpass);
echo round(abs($today-$dayinpass)/60/60);
You can use strtotime() to parse your strings and do the difference between the two of them.
Resources :
php.net - strtotime()

Categories