This question already has answers here:
How to get time difference in minutes in PHP
(21 answers)
Closed 3 years ago.
I want to compare two times which is in 12hrs format, but before comparing I have converted into strtotime and passed converted
values to function and comparing it but output return 0 even there is time difference of 60 minutes still getting 0 in output.
<?php
function timediff($start, $end)
{
if($end >= $start) {
return (round(($end-$start)/3600, 0))." Hrs ".((($end-$start)%3600)/60)." Min"; // time should be in queue
} else {
return (round(($start-$end)/3600, 0))." Hrs ".((($start-$end)%3600)/60)." Min";
}
}
echo timediff(strtotime('09: am'), strtotime('08: am'));
you should pass a valid time string, try it
function timediff($start, $end) {
if ($end >= $start) {
return (round(($end - $start) / 3600, 0)) . " Hrs " . ((($end - $start) % 3600) / 60) . " Min";
} else {
return "-" . (round(($start - $end) / 3600, 0)) . " Hrs " . ((($start - $end) % 3600) / 60) . " Min";
}
}
echo timediff(strtotime('09:00 am'), strtotime('08:00 am'));
Related
This question already has answers here:
How do I find the hour difference between two dates in PHP?
(10 answers)
Closed 5 years ago.
I have datetimes in the following format:
Start: 2017-07-16 20:00
End: 2017-07-16 23:30
Start: 2017-07-18 21:30
End: 2017-07-19 00:30
I need to tell from these intervals how many hours (in 0,5 increments) are spent between 18:00-22:00 and 22:00-06:00 in total for a month.
Thanks in advance for any hint.
The current code I have is this, but I'm not sure if it is covering all timeframe possibilities:
<?php
date_default_timezone_set("UTC");
$start = array(
"2017-07-16 21:30:00",
"2017-07-16 18:00:00"
);
$end = array(
"2017-07-17 00:30:00",
"2017-07-16 18:30:00"
);
$amount_low = 0;
$amount_high = 0;
for($i = 0; $i < sizeof($start); $i++) {
$start_time = date("H:i:s", strtotime($start[$i]));
$end_time = date("H:i:s", strtotime($end[$i]));
$start_date = date("Ymd", strtotime($start[$i]));
$end_date = date("Ymd", strtotime($end[$i]));
// getting chunk before 22:00 if
if(
(strtotime($start[$i]) >= strtotime($start_date . " 18:00") && strtotime($start[$i]) < strtotime($start_date . " 22:00"))
&&
$start_date < $end_date
) {
$interval_low = strtotime($start_date . " 22:00") - strtotime($start[$i]);
$amount_low += ceil($interval_low / 1800) / 2;
}
//amount_high
if(strtotime($start[$i]) > strtotime($start_date . " 22:00") && strtotime($start[$i]) < strtotime($start_date . " 06:00")) {
$interval_high = strtotime($end[$i]) - strtotime($start[$i]); //needs further things
$amount_high += ceil($interval_high / 1800) / 2;
} elseif (strtotime($start[$i]) < strtotime($start_date . " 22:00") && strtotime($end[$i]) > strtotime($start_date . " 22:00")) {
$interval_high = strtotime($end[$i]) - strtotime($start_date . " 22:00");
$amount_high += ceil($interval_high / 1800) / 2;
} else {
$interval_low = strtotime($end[$i]) - strtotime($start[$i]);
$amount_low += ceil($interval_low / 1800) / 2;
}
}
echo $amount_low;
echo "\n$amount_high";
?>
Would this work for you?
$start = strtotime('2017-07-16 20:00');
$end = strtotime('2017-07-16 23:30');
$interval = $end - $start;
$hours = floor($interval / 1800)/2;
echo($hours);
This will display the total hours (in 0,5 increments) between the two dates (rounding down, so if it's 55 minutes, it's 0,5 hours; replace 'floor' with 'ceil' for the opposite).
I believe a part of your solution is found here from Aillyn
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.
$hourdiff = round((strtotime($time1) - strtotime($time2))/3600, 1);
This will give you a rounded hour difference between the two times. You could do something similar, just apply it for each interval for which ever month and adjust your math. There's not really going to be a single PHP function you can call to solve this.
I have a time left function that I am using to get the time left based on a sent parameter. My issue is I am having difficulty calculating if there is a day, a year, or a month left.
Function:
function get_time_difference_php_left($created_time)
{
$str = strtotime($created_time);
$today = strtotime(date('Y-m-d H:i:s'));
// It returns the time difference in Seconds...
$time_differnce = $today-$str;
// To Calculate the time difference in Years...
$years = 60*60*24*365;
// To Calculate the time difference in Months...
$months = 60*60*24*30;
// To Calculate the time difference in Days...
$days = 60*60*24;
// To Calculate the time difference in Hours...
$hours = 60*60;
// To Calculate the time difference in Minutes...
$minutes = 60;
if(intval($time_differnce/$years) > 1)
{
return " - ". intval($time_differnce/$years)." years left";
}else if(intval($time_differnce/$years) > 0)
{
return " - ".intval($time_differnce/$years)." year left";
}else if(intval($time_differnce/$months) > 1)
{
return " - ".intval($time_differnce/$months)." months left";
}else if(intval(($time_differnce/$months)) > 0)
{
return " - ".intval(($time_differnce/$months))." month left";
}else if(intval(($time_differnce/$days)) > 1)
{
return " - ".intval(($time_differnce/$days))." days left";
}else if (intval(($time_differnce/$days)) > 0)
{
return " - ".intval(($time_differnce/$days))." day left";
}else if (intval(($time_differnce/$hours)) > 1)
{
return " - ".intval(($time_differnce/$hours))." hours left";
}else if (intval(($time_differnce/$hours)) > 0)
{
return " - ".intval(($time_differnce/$hours))." hour left";
}else if (intval(($time_differnce/$minutes)) > 1)
{
return " - ".intval(($time_differnce/$minutes))." minutes left";
}else if (intval(($time_differnce/$minutes)) > 0)
{
return " - ".intval(($time_differnce/$minutes))." minute left";
}else if (intval(($time_differnce)) > 1)
{
return " - ".intval(($time_differnce))." seconds left";
}else
{
return " - few seconds left";
}
}
If I run this based on the now time and a date time of: 2014-04-17 03:27:26 it will tell me 88 years.
Suggestions, thoughts?
You had March initially. You are doing the calculation backwards then. Your code should show expiration date, not creation date. If you are trying to use an expiration date, then your code should use:
$time_differnce = $str - $today;
You can use the modulus (remainder) operator in each if to show the next value as well. For example if you have 3.7 days, you use 3 days and then .7*$hours.
Change all else if statements to if's
Change from:
if(intval($time_differnce/$years) > 1) {
return " - ". intval($time_differnce/$years)." years left";
} else if (intval($time_differnce/$years) > 0) {
to
if(intval($time_differnce/$years) > 1) {
return " - ". intval($time_differnce/$years)." years left";
}
if (intval($time_differnce/$years) > 0) {
This question already has answers here:
Convert number of minutes into hours & minutes using PHP
(14 answers)
Closed 9 years ago.
I wonder if there is a function in php or codeigniter which can do this :
function transformTime($min)
{
$min=(int)$min;
$heure=(int)($min/60);
$minute=(($min/60)-$heure)*60;
return $heure .':' . $minute . ':00';
}
I want convert x minutes to a time format.
Do something like this
<?php
function transformTime($min)
{
$ctime = DateTime::createFromFormat('i', $min);
$ntime= $ctime->format('H:i:s');
return $ntime;
}
echo transformTime(60); // "Prints" 01:00:00
You are almost there, just format the output with sprintf() or str_pad():
function transformTime($min)
{
$hour = floor($min / 60);
$min -= $hour * 60;
return sprintf('%02d:%02d:00', $hour, $min);
}
This question already has answers here:
Converting timestamp to time ago in PHP e.g 1 day ago, 2 days ago...
(32 answers)
Closed 9 years ago.
I have a code
echo date('d-m-Y H:i', strtotime($posts_row['post_date'])) . '</td></tr>';
The result:
23-10-2013 16:28
Is there a way to change into like:
Posted 12 minutes ago
or / &
Just Posted
Whats the easiest way of doing this?
strtotime will help you to convert the date and make operations over it.
function elapsedTimeAgo ($newTime) {
$timeCalc = time() – strtotime($newTime);
$elapsedTimeText = "";
if ($timeCalc > (60*60*24)) {
$elapsedTimeText = round($timeCalc/60/60/24) . "days ago";
} else if ($timeCalc > (60*60)) {
$elapsedTimeText = round($timeCalc/60/60) . "hours ago";
} else if ($timeCalc > 60) {
$elapsedTimeText = round($timeCalc/60) . "minutes ago";
} else if ($timeCalc > 0) {
$elapsedTimeText .= "seconds ago";
} else {
$elapsedTimeText .= "Just Posted";
}
return $elapsedTimeText;
}
echo elapsedTimeAgo($posts_row['post_date']);
To fetch the difference of seconds between current time and posted time:
$number_of_seconds = strtotime("now") - strtotime($posts_row['post_date']);
Then, you can apply your logic to display how many seconds ago, just now, etc.
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 11 years ago.
I've got a piece of code that I created and I've been 'requiring_once' in to my PHP code for quite a while now. It works fine on my local PC, but when I upload it to my server it's coming up with the following error:
Parse error: syntax error, unexpected T_ELSE in /home/makequiz/public_html/nitpicker/func.php on line 47
Here's the function:
function howLongAgo($unix_time) {
// This function shows the unix time stamp
// as number of seconds, minutes, hours, days, months, years ago
// Get the time difference between time specified and the current unix time
$time_difference = time () - $unix_time;
$seconds_ago = $time_difference;
$minutes_ago = $time_difference / 60;
$hours_ago = $time_difference / 3600;
$days_ago = $time_difference / 86400;
$months_ago = $time_difference / 2629743;
$years_ago = $time_difference / 31556926;
if ($time_difference < 60) {
// Seconds Ago
return round ( $time_difference ) . ' Seconds Ago';
} else if ( ($time_difference >= 60) && ($time_difference < 3600) ) {
// Minutes Ago
if (round ( $time_difference / 60 ) == 1) {
return round ( $time_difference / 60 ) . " Minute Ago";
} else {
return round ( $time_difference / 60 ) . " Minutes Ago";
}
} else if ($time_difference >= 3600 && $time_difference < 86400) {
// Hours Ago
if (round ( $time_difference / 3600 ) == 1) {
return round ( $time_difference / 3600 ) . " Hour Ago";
} else {
return round ( $time_difference / 3600 ) . " Hours Ago";
}
} else if ($time_difference >= 86400 && $time_difference < 2629743) {
// Days Ago
if (round ( $time_difference / 86400 ) == 1) {
return round ( $time_difference / 86400 ) . " Day Ago";
} else {
return round ( $time_difference / 86400 ) . " Days Ago";
}
} else if ($time_difference >= 2629743 && $time_difference < 31556926) {
// Months Ago
if (round ( $time_difference / 2629743 ) == 1) {
return round ( $time_difference / 2629743 ) . " Month Ago";
} else {
return round ( $time_difference / 2629743 ) . " Months Ago";
}
} else if ($time_difference >= 31556926) {
// Years Ago
if (round ( $time_difference / 31556926 ) == 1) {
return round ( $time_difference / 31556926 ) . " Year Ago";
} else {
return round ( $time_difference / 31556926 ) . " Years Ago";
}
}
}
This error is coming up, even though I haven't called the function at all in my code.
Can anyone see any errors in the code, cause I can't :(
This would be a lot simpler using a switch/case logic scheme. I have re-coded what you had up there using switch/case and this might be a good intro for you to learn switch/case. You had some extra ( ) in one of the if statements above. I hope this helps, friend:
<?
$unix_time = 6734;
echo howLongAgo($unix_time);
function howLongAgo($time_difference){
// Swtich logic based on the time difference passed to this function, sets the english string and what number the difference needs to be divided by
switch($time_difference){
case ($time_difference < 60):
$string = " second";
break;
case ($time_difference >= 60 && $time_difference < 3600):
$string = " minute";
$divider = 60;
break;
case ($time_difference >= 3600 && $time_difference < 86400):
$string = " hour";
$divider = 3600;
break;
case ($time_difference >= 86400 && $time_difference < 2629743):
$string = " day";
$divider = 86400;
break;
case ($time_difference >= 2629743 && $time_difference < 31556926):
$string = " month";
$divider = 2629743;
break;
case ($time_difference >= 31556926):
$string = " year";
$divider = 31556926;
break;
}
// If a divider value is set during the switch, use it to get the actual difference
if($divider){$diff = round($time_difference / $divider);}else{$diff = round($time_difference);}
// If the difference does not equal 1, pluralize the final result EG: hours, minutes, seconds
if($diff != 1){$pluralize="s";}
// Concatenate all variables together and return them
$final = $diff . $string . $pluralize . " ago";
return $final;
}
?>
I think your problem may come from the difference between elseif and else if (note the space).
From the PHP website:
Note: Note that elseif and else if will only be considered exactly the same when using curly brackets as in the above example. When using a colon to define your if/elseif conditions, you must not separate else if into two words, or PHP will fail with a parse error.
You have a syntax error in your code at line 47 (I am going to bet you are missing a semi-color or bracket at line 46.
It does not matter if you are calling the function or not, as the php parser will analyze the entire file upfront.
Couple of things..
did you change the file that it's included in when you loaded to your server?
try using include instead of require?