How to get 1 hours before [duplicate] - php

This question already has answers here:
Create Variable in PHP Equal to Current Time Minus One Hour
(8 answers)
Closed 3 years ago.
how to get 1 hours before in php
I try with:
but not working
$jam = "10:00:00";
$1hrsbefore = strtotime($jam), strtotime(-1 hours);

Try this code :
$jam = "10:00:00";
$timeadd= strtotime($jam) - 60*60;
$time = date('H:i:s', $timeadd);
echo $time;

Try this
$jam="10:00:00";
$time = date('H:i:s', strtotime($jam) - 60*60);
echo $time;

Related

Add string variable (minutes) to time variable in php [duplicate]

This question already has answers here:
PHP Adding 15 minutes to Time value
(9 answers)
Closed 3 years ago.
I got some problems handling string and time.
I am reading a form which gives me a string like this: "08:00"
Now i am running a foreach loop after which i want to add e.g. 15 minutes to the upper string.
I tried to convert the "08:00" to a time with
$string = "08:00";
$time = date("H:i", strtotime($string));
echo $time; //echos 1577260800
How can i add e.g. 15 minutes or even better a string like $add = "10" to the $time? The following doesnt work.
$add = "10";
$newtime = $time + strtotime($add);
Just add time in seconds to an existing time.
$string = "08:00";
$timeInSeconds = strtotime($string) + 15*60; // 15*60 => 15 minutes in seconds
$time = date("H:i", $timeInSeconds );
echo $time; // shows 8:15
You can just use the strtotime's parsing of words.
Meaning you can just as you ask "add 10 minutes".
$string = "08:00";
$time = date("H:i", strtotime($string . " +10 minutes"));
echo $time; //8:10

PHP Time Difference [duplicate]

This question already has answers here:
How to get time difference in minutes in PHP
(21 answers)
Closed 4 years ago.
I have 2 dates in mysql:
2018-07-13 13:00:00 - data
2018-07-14 16:01:00 - godzina
$timestamp1 = strtotime($data['data']);<br>
$timestamp2 = strtotime($data['godzina']);<br>
$time_difference = $timestamp2 - $timestamp1;
$time_total = ($time_difference/3600);
echo "td".$time_total."/td";
Now it returns: 27.016666666667
How to change it to: 27:01 in table?
You can simply use DateTime->diff method to get the interval between those 2 dates:
$date1 = '2018-07-13 13:00:00';
$date2 = '2018-07-14 16:01:00';
$interval = (new DateTime($date2))->diff(new DateTime($date1));
$totalHours = $interval->days * 24 + $interval->h;
echo sprintf("%02d", $totalHours) . ':' . sprintf("%02d", $interval->i);

How to sum up time in php? [duplicate]

This question already has answers here:
PHP add up two time variables
(7 answers)
Closed 7 years ago.
How to sum up time in php.
For example I have this series of time duration logs:
00:10:00
00:30:10
01:00:50
The total should be 1 hour and 41 minutes
Here is my code:
$log_in = new DateTime($log->log_in);
$log_out = new DateTime($log->log_out);
$diff = $log_out->diff($log_in);
$total += strtotime($diff->format('%H:%i:%s'));
echo $diff->format('%H:%i:%s');
Convert the time into timestamp using strtotime() function. Then manipulate the time according your need and get result in terms of seconds.
Once you get seconds.
For Hour
$hour = $diff % 3600
For Minute
$Minute = ($diff - ( $hour *3600))%60;

calculate the difference between two dates with strtotime [duplicate]

This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 8 years ago.
I want to echo how much days are left between a variable and NOW. This is the code
$now = date('Y-m-d');
$daysleft= strtotime($starttheproject -$now);
echo
"Now =".$now.
"<br> Starttheproject =".$starttheproject.
"<br> Daysleft =".$daysleft;
Result of echo:
Row =2014-10-17
Starttheproject =2014-10-22
Daysleft =
Question:
How can calculate the number of days, so that the result will be '5'
I've been playing with code like this, with no luck:
$daysleft= date('Y-m-d', strtotime($starttheproject -$now));
Try with this:
$date = new DateTime('2014-03-10');
$finish = new DateTime();
$difference = $date->diff($finish);
$difference =$difference->format('%R%a');
if ($difference > 10) { //if the post is over 10 days old
echo 'Renew now';
}else{
echo 'Renew in '.$difference.' days</font>';
}

Php calculete the cost for each day passed [duplicate]

This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
PHP calculate days Between 2 different dates [duplicate]
(2 answers)
Closed 9 years ago.
$1date =$row['Date1'];
$2date = $row['Date2'];
$datediff = $1date - $2date;
echo $datediff;
I want to count the days between and put into a table the result(10 dollars for each day passed)
// convert to unix timestamp
$1date = strtotime($row['Date1']);
$2date = strtotime($row['Date2']);
// 86400 seconds in a day
// floor to round down, change to ceil to round up
$datediff = floor(($1date - $2date) / 86400);
$cost = $days * 10;
You could do it in MySQL then work it into a variable. Do the initial call
SELECT TIMESTAMPDIFF('Date1','Date2');
Try:
$days = date_diff(date_create($row['Date1']), date_create($row['Date2']))->format('%a');
$cost = $days * 10;

Categories