This question already has answers here:
display hours in half hour range
(3 answers)
Closed 3 years ago.
I have Two fields like time-from and time-to and time-took for particular people so my job is
ex: time-from: 10:00:00
time-to : 1:20:10
time-took : 30 (min)
So i want to display the order like that:
10:00:00 , 10:30:00 11:00:00, 11:30:00, 12:00:00, 12:30:00, 1:00:00, 1:20:10
If you convert the times into a time stamp using the PHP function "mktime", you then have the number of seconds difference between the dates. If the date is irreverent, then you could just use the same day for all of the times.
// The time to get the number of seconds from.
$tmpStr = "01:10:20";
// Break the string into components.
$timeArray = split(":", $tmpStr);
// The date element is redundant but needs to be set.
$timeInSeconds = mktime ($timeArray[0], $timeArray[1], $timeArray[2], 1, 1, 1970);
Now having the time in seconds from midnight allows you to order them accordingly and to also compare two times to find out the difference.
Related
This question already has answers here:
Return current date plus 7 days
(10 answers)
Closed 2 years ago.
So I have the below property where I'm passing in a time()-86000 value, but what would be the best call to generate a timestamp from the time the class method executes and then adds 7 days to that.
Here is what I got:
$profile->set_picture_expiration(time()-86000)
Would using time()-86000 be the right call? I'd like to write it to the DB in timestamp format from the current time + 7 days.
First of all, if you need +7 days, why are you using minus?
Next, 7 days are 7*24*60*60 = 604800 seconds, not 86000.
Finally, the easiest way to get the timestamp for such relative dates is using the strtotime function. In your specific case it would be strtotime('+7 days').
$profile->set_picture_expiration(strtotime('+7 days'));
This question already has answers here:
adding 30 minutes to datetime php/mysql
(6 answers)
PHP Adding 15 minutes to Time value
(9 answers)
Closed 2 years ago.
So basically I need to add 30mins to the current time and send it as a time stamp in database.
date_default_timezone_set('Asia/Hong_Kong');
$today = date("H:i:s");
I.E i need current time : 10:00 sent time to database 10:30.
I know the codes in sending it to the database i just want to know how to add certain mins or seconds to the current time.
You would use the database time for this:
now() + interval 30 minute
You could set a column with the value as:
update t
set col = now() + interval 30 minute
where id = 1;
Pretty simple, but go with the database solution if possible:
$today = date("H:i:s", strtotime(“+30 minutes”);
This question already has answers here:
How to get time difference in minutes in PHP
(21 answers)
Closed 4 years ago.
I have 2 outputs from following code:
$start_time = $dt->format('Y-m-d H:i:s');
$end_time = $dt->format('Y-m-d H:i:s');
start_time - 2018-09-27 17:42:01
end_time - 2018-09-27 17:42:16
How can i get the difference in minutes, even if the time diff is below 1 minute, like example 0.5 minutes (30 seconds).
I require only minutes value, even if it is part of a minute. How is it possible in php using time functions?
Thanks in advance.
$differenceInMinutes = (strtotime($end_time) - strtotime($start_time))/60
This converts your 2 dates into a unix time first by using strtotime() (PHP documentation). The unix time is always in seconds. Then when we subtract them from each other we get the difference in seconds between the two dates. To get from seconds to minutes you can of course divide by 60.
This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 6 years ago.
I want to count how many hours, minutes and seconds between two dates.
For example: Date 1: 01-01-1990 12:00 and 01-02-1990 13:00
Now, how many hours, minutes, and second from date 1 to date 2 using php?
just use strtotime() on your dates to their timestampz . and then subtract them to have the difference in the number of seconds. it's easy to convert from there
This question already has answers here:
Calculate number of hours between 2 dates in PHP
(17 answers)
Closed 9 years ago.
Is there any recommendation of datetime calculation?
I have been going toward UNIX timestamp and diff calculation method
but both of it don't really provide a better way to complete my task.
Here is the sample of datetime I have.
2013-09-07 21:12
I wish to calculate the duration between the 2 datetime.
Lastly display the total of minutes duration for the 2 datetime.
Most of the calculation work to display for year, months, days, hours, minutes, seconds.
I need some method to find out the duration between 2 times and convert the duration into total of minutes.
Anyone?
Use function mktime to get the UNIX timestamp of the two dates, then take the bigger one and subtract the smaller one. You'll get another value, if you divide that value by 60, you'll get how many minutes are inbetween the two dates.