Trouble setting interval - php

function crimemaketime($until){
$now = time();
$difference = $until - $now;
$days = floor($difference/86400);
$difference = $difference - ($days*86400);
$hours = floor($difference/3600);
$difference = $difference - ($hours*3600);
$minutes = floor($difference/60);
$difference = $difference - ($minutes*60);
$seconds = $difference;
$output = "$minutes Minutes and $seconds Seconds";
return $output;
}
Hi, im looking to set and interval so that no refreshing is need for my timers.
This i have above works fine for my output, but im unable to get it to work with setinterval.
ive searched the internet for a good few hours and nothing has seemed to work or maybe im doing something wrong.
Any help is appreciated, many thanks.

Related

Calculate time difference with formatted time

I have two times: a starting time and the duration. I want to subtract the duration from the starting time. The time I read from an mysql db and is already formatted. My code:
$start= $row["start"]; //output is for e.g. 08:00:00
$dur = $row["duration"]; //output is for e.g. 01:00:00
$sub = $start - $dur;
// I want the output to be 07:00:00
// the result now is 7 and I got an error (non well formed numeric value)
Can someone help me?
Alternatively you can achieved like this
$date = "1970-01-01";
$start = $date." ".$row["start"];
$dur = $date ." ".$row["duration"];
$date1=date_create($start);
$date2=date_create($dur);
$diff=date_diff($date1,$date2);
echo $diff->format("%H:%I:%S");
$start= "08:00:00";
$dur = "01:00:00";
$diff = differenceInHours($start, $dur);
echo convertTime($diff);
function differenceInHours($startdate,$enddate){
$starttimestamp = strtotime($startdate);
$endtimestamp = strtotime($enddate);
$difference = abs($endtimestamp - $starttimestamp)/3600;
return $difference;
}
function convertTime($dec)
{
// start by converting to seconds
$seconds = ($dec * 3600);
// we're given hours, so let's get those the easy way
$hours = floor($dec);
// since we've "calculated" hours, let's remove them from the seconds variable
$seconds -= $hours * 3600;
// calculate minutes left
$minutes = floor($seconds / 60);
// remove those from seconds as well
$seconds -= $minutes * 60;
// return the time formatted HH:MM:SS
return lz($hours).":".lz($minutes).":".lz($seconds);
}
// lz = leading zero
function lz($num)
{
return (strlen($num) < 2) ? "0{$num}" : $num;
}
You should always save date in your database in the appropriate format. And not as a "already formated" value. Except in very specific case.
Anyway, to solve your problem you can do soemthing like this
$start = new DateTime('08:00:00');
$duration = new DateTime('01:00:00');
$interval = date_diff($start, $duration);
echo $interval->format('%H:%I:%S'); //ouput will be 07:00:00

Add up time from DateTime in PHP

I'll try to explain my problem. I am beginner with PHP and need help manipulating Date/Time.
So here is my situation.
I get Date/Time values from the database in this format: 07/02/2017 11:00 pm
First I need to calculate a difference between two dates and output duration.
Then add up duration and output total time.
The code is very dirty as I am just researching now. I also came to a problem that DateTime does no carry over points. As far as understand I need to convert days to hours and add them up.
Can anybody more experienced make sense of this?
$total_time = new DateTime('00:00');
$start_date = new DateTime('2017-05-01 12:20 PM');
$end_date = new DateTime('2017-05-01 12:30 PM');
$interval = $start_date->diff($end_date);
$total_days = $interval->days;
$hours = $interval->h;
if ($total_days !== FALSE) {
$hours += 24 * $total_days;
}
$minutes = $interval->i;
$total_time ->add($interval);
echo $hours .'hours ' . $minutes . 'minutes';
echo $total_time->format('h:i'); ?>
You can add a DateInterval to the DateTime.
You can check on http://php.net/manual/en/datetime.add.php
Looking at your code:
$total_hours = 0;
$total_minutes = 0;
//must be initialized outside of the while loop
$start_date = new DateTime('2017-05-01 12:20 PM');
$end_date = new DateTime('2017-05-01 12:30 PM');
$interval = $start_date->diff($end_date);
$hours = $interval->h + 24*$interval->d; /*there is no need to check
if you have total days, since 0 days would still work as expected */
$minutes = $interval->i;
echo 'Duration: '.$hours.' hours '.$minutes.' minutes';
$total_hours += $hours;
if(($total_minutes += $minutes) >= 60) {
$total_hours += 1;
$total_minutes -= 60;
}
//after the end of the while loop
echo 'Total time:'.$total_hours.':'.$total_minutes;
Now I don't understand what is the expected output of total time, if you can elaborate on what is not working it would be easier to help

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

Getting wrong time after subtraction

Hello I am using strtotime function to subtract time but I am not able to getting such true value of it.
$time = strtotime('00:00:00');
$time1 = strtotime('23:00:00');
$t = $time - $time1;
echo "diff".$t1 = date('H:i:s',$t);
But am getting $t = 2:00:00 Instead of i get 1:00:00
Any one have idea whats gone wrong ?
try to add strtotime() on changing format
date('H:i:s',strtotime($t));
$time = strtotime('00:10:00');
$time1 = strtotime('23:00:00');
$t = $time - $time1;
$hours = floor(($t - (($t/ 86400)* 86400)) / 3600);
$minutes = floor(($t - (($t/ 86400) * 86400) - ($hours * 3600))/60);
echo "".$hours.':'.$minutes;
its give you right diff. time for same days .

Php get how many days and hours left from a date

I have a created_at date saved liked this "2011-09-23 19:10:18" And I want to get the days and hours left
until the date is reached. How do I do that? and column name in database remain days automatically update daily with remain days, please solve this
PHP fragment:
<?php
//Convert to date
$datestr="2011-09-23 19:10:18";//Your date
$date=strtotime($datestr);//Converted to a PHP date (a second count)
//Calculate difference
$diff=$date-time();//time returns current time in seconds
$days=floor($diff/(60*60*24));//seconds/minute*minutes/hour*hours/day)
$hours=round(($diff-$days*60*60*24)/(60*60));
//Report
echo "$days days $hours hours remain<br />";
?>
Note the hour-round and no minutes/seconds consideration means it can be slightly inaccurate.
This should seed your endeavor.
getdate(strtotime("2011-09-23 19:10:18"))
Full conversion:
$seconds = strtotime("2011-09-23 19:10:18") - 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";
as of PHP 5.3.0 you could use build-in Date object:
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
http://www.php.net/manual/en/book.datetime.php
it would be something like
echo $date = date("Y-m-d H:i:s");echo "\n";
$original=time($date);
$modified = "2011-09-23 19:10:18";
echo date("Y-m-d H:i:s",$modified);echo "\n";
The easiest way is improved answer from CountZero. I used this solution for counter for time remaining before expiration of offer. Addition to first three lines of CountZero code:
$days = $interval->d;
$hours = $interval->h;
$minutes = $interval->i;
$seconds = $interval->s;
And now, you can use string functions to moderate your return values, to merge all or add '0' or '00' in front of the values.
You can find current date and time using date() function and then subtract
$x = 2011-09-23 - current_date
this will give you no. of days left.
Do same with time as well ..
Hope this helps

Categories