I have countdown on my site which fails to refresh itself.
Code: http://pastebin.com/DNjb0WSN
window.onload = function() {
idElement = "time";
document.getElementById(idElement).innerHTML =
"<?php countdown(21,00,00,9,15,2012) ?>";
setInterval("document.getElementById(idElement).innerHTML =
'<?php countdown(21,00,00,9,15,2012) ?>'", 1000);
};
// ...
<?php
function countdown($godzina, $minuta, $sekunda, $miesiac, $dzien, $rok) {
$target = mktime($godzina, $minuta, $sekunda, $miesiac, $dzien, $rok);
$now = time();
$sekundy = ($target - $now);
// $sekundy =(int) ($sekundy) ;
$check = 0;
if ($sekundy > 0) {
if ($check == 0) {
if ($sekundy <= 10000) {
// zmien klase
$check = 1;
}
}
$hours = floor($sekundy / 3600);
$minutes = floor(($sekundy / 60) - ($hours * 60));
$seconds = floor(($sekundy) - ($hours * 3600) - ($minutes * 60));
if ($hours < 10) $hours = '0'.$hours;
if ($minutes < 10) $minutes = '0'.$minutes;
if ($seconds < 10) $seconds = '0'.$seconds;
$all = $hours." : ".$minutes." : ".$seconds;
echo $all;
}
else {
echo "Aukcja zakończona!";
}
}
?>
Why it doesn't work?
Because the countdown is not implemented in javascript but php. If you need the countdown to work on the client you need to implement it in javascript or poll the server(NOT RECOMMENDED).
The web is stateless, does that ring any bells?
Try using a countdown library written in JavaScript and parametrize it from PHP:
http://www.hashemian.com/tools/javascript-countdown.htm
Related
I have to complete 540 mins i.e 9 hours 00 minutes for today and my today in time is 11:10 AM, So I can go after 6.10pm ie my out time
function convert($time, $format = '%d:%d') {
settype($time, 'integer');
if ($time < 1) {
return;
}
$hours = floor($time / 60);
$minutes = ($time % 60);
return sprintf($format, $hours, $minutes);
}
$remain_min = 540;
$remain_time = convert($remain_min, '%02d hours %02d minutes');
echo 'You have to complete '.$remain_time.' for this week. ';
$in_timeh =11;
$in_timem = 10;
$timeformat = 'AM';
echo "Your in time" . $in_timeh. ":" . $in_timem . $timeformat . "<br />";
How to calculate it ?
I have tried this, but not seems to good, plz anyone help with better suggetion
if($timeformat == "pm"){
$in_timeh += 12;
}
$in_time_minutes = ($in_timeh * 60) + $in_timem;
$total_minutes_today = $remain_min + $in_time_minutes;
$total_minutes_today1 = floor($total_minutes_today/60).":".($total_minutes_today%60);
$newDateTime = date('h:i A', strtotime($total_minutes_today1));
echo "You can go after" . $newDateTime;
Firstly make your both in-time and out-time to time to string, and then passon that values to the following function, it will return to you the difference time.
function timeBetween($start_date,$end_date)
{
$diff = $end_date-$start_date;
$seconds = 0;
$hours = 0;
$minutes = 0;
if($diff % 86400 <= 0){$days = $diff / 86400;} // 86,400 seconds in a day
if($diff % 86400 > 0)
{
$rest = ($diff % 86400);
$days = ($diff - $rest) / 86400;
if($rest % 3600 > 0)
{
$rest1 = ($rest % 3600);
$hours = ($rest - $rest1) / 3600;
if($rest1 % 60 > 0)
{
$rest2 = ($rest1 % 60);
$minutes = ($rest1 - $rest2) / 60;
$seconds = $rest2;
}
else{$minutes = $rest1 / 60;}
}
else{$hours = $rest / 3600;}
}
if($days > 0){$days = $days.' days, ';}
else{$days = false;}
if($hours > 0){$hours = $hours.' hours, ';}
else{$hours = false;}
if($minutes > 0){$minutes = $minutes.' minutes, ';}
else{$minutes = false;}
$seconds = $seconds.' seconds';
return $days.''.$hours.''.$minutes.''.$seconds;
}
try this
$date=date_create("09:00");
date_add($date,date_interval_create_from_date_string("540 minutes"));
echo date_format($date,"H:i");
You can use
$add = date("H:i:s", strtotime('+9 hours'));
echo "You can go after".$add;
I hope this helps you.
<?php
$mystartTime = "11:10:02 AM"; //hour: minute:seconds
$hrs = 60 * 60 * 9;
$mystartTimeSecs = strtotime($mystartTime);
$outTime = date('h:i:s A', $mystartTimeSecs + $hrs);
echo "You can go at " . $outTime;
echo "<br />";
$left = $mystartTimeSecs + $hrs;
$remainingTime = $left - time();
$hours = floor($remainingTime / 3600);
$minutes = floor(($remainingTime / 60) % 60);
$seconds = $remainingTime % 60;
echo "<br />";
echo "Time Left: $hours $minutes $seconds";
echo "<br />";
echo "Time Left: " . date("h:i:s", $remainingTime);
?>
The code is
$la=time()-$row['laston'];
$unit="secs";
if($la >= 60)
{
$la=(int) ($la/60);
$unit="mins";
}
if($la >= 60)
{
$la=(int) ($la/60);
$unit="hours";
if($la >= 24)
{
$la=(int) ($la/24);
$unit="days";
}
print "<li><a class=\"button\" href=\"user.php?p={$row['uid']]'}\">{$row['display_name']} ~($la $unit)</a></li>";
and $row['laston']; is stored like: 1392490566
My problem is it is showing like:
User1 ~(-58secs) when i refresh the page, I'd expect it to be 0-2 secs not a negative value?
Could someone please help me understand what is causing this?
and would changing $la=time()-$row['laston']; to $la=time()-$row['laston']+58; be an appropriate fix?
Try this:
$la = time() - $row['laston'];
if($la > 0) {
$newLa = $la / 24;
$unit = "days";
} if($la < 84600) {
$newLa = $la / 60;
$unit = "hours";
} if($la < 3600) {
$newLa = $la / 60;
$unit = "mins";
} if($la < 60) {
$newLa = $la;
$unit = "secs";
}
echo round($newLa, 2) . ' ' . $unit;
Sorry for the slightly misleading title, I'm not really sure how to word this.
Basically, I'm working on a time clock-in system.
I've got the data for each clock in: clock in timestamp and clock out timestamp.
After all of the clockins have been displayed in a specific period, the script adds up all of the differences between the two timestamps.
The one thing I need to do now, is actually convert this figure into hours and minutes.
Use this function from more detailed output
function datefor($date, $time)
{
$days = floor($time / (60 * 60 * 24));
$remainder = $time % (60 * 60 * 24);
$hours = floor($remainder / (60 * 60));
$remainder = $remainder % (60 * 60);
$minutes = floor($remainder / 60);
$seconds = $remainder % 60;
if($days > 0) {
$data = date('F d Y', $date);
reset($date);
}
elseif($days == 0 && $hours == 0 && $minutes == 0) {
$data = "few seconds ago";
}
elseif($days == 0 && $hours == 0) {
$data = $minutes.' minutes ago';
}
elseif($days == 0 && $hours > 0) {
$data = $hours.' hour ago';
}
else {
$data = "few seconds ago";
}
return $data;
}
and select using the following statement
SELECT *,UNIX_TIMESTAMP() - datetime AS TimeSpent FROM `table_name`;
$result=mysql_query($sql);
while($rows=mysql_fetch_array($result)){
//call function
$check = datefor($rows['datetime'], $rows['TimeSpent']);}
now echo $check; where you want the time to be displayed.
<strong>So far, you have worked <?php
$hours = floor($i / 3600);
$i -= 3600 * floor($i / 3600);
$minutes = floor($i / 60);
echo $hours; ?> hours and <?php echo $minutes; ?> minutes</strong>
yep, it works.
I'm using Codeigniter's "timespan()" function to give a human readable phrase of how much time has elapsed since an event: "5 days, 3 hours, 12 minutes, 1 second", but I don't need to go down to the minute or second level.
Is there any way to turn that off?
Short answer: no.
The timespan() method doesn't provide any means to customize the level of detail in its output. CodeIgniter does, though, make it pretty easy to override built-in functions so they do what you like.
There may be a more elegant solution (see below), but the following is what I came up with. Create a file called MY_date_helper.php in application/helpers and put the following modification to timespan() into it:
<?php
// Adds a third argument to timespan() that stops display of minutes/
// seconds in the final output
function timespan($seconds = 1, $time = '', $display_mins_secs = true)
{
$CI =& get_instance();
$CI->lang->load('date');
if ( ! is_numeric($seconds))
{
$seconds = 1;
}
if ( ! is_numeric($time))
{
$time = time();
}
if ($time <= $seconds)
{
$seconds = 1;
}
else
{
$seconds = $time - $seconds;
}
$str = '';
$years = floor($seconds / 31536000);
if ($years > 0)
{
$str .= $years.' '.$CI->lang->line((($years > 1) ? 'date_years' : 'date_year')).', ';
}
$seconds -= $years * 31536000;
$months = floor($seconds / 2628000);
if ($years > 0 OR $months > 0)
{
if ($months > 0)
{
$str .= $months.' '.$CI->lang->line((($months > 1) ? 'date_months' : 'date_month')).', ';
}
$seconds -= $months * 2628000;
}
$weeks = floor($seconds / 604800);
if ($years > 0 OR $months > 0 OR $weeks > 0)
{
if ($weeks > 0)
{
$str .= $weeks.' '.$CI->lang->line((($weeks > 1) ? 'date_weeks' : 'date_week')).', ';
}
$seconds -= $weeks * 604800;
}
$days = floor($seconds / 86400);
if ($months > 0 OR $weeks > 0 OR $days > 0)
{
if ($days > 0)
{
$str .= $days.' '.$CI->lang->line((($days > 1) ? 'date_days' : 'date_day')).', ';
}
$seconds -= $days * 86400;
}
$hours = floor($seconds / 3600);
if ($days > 0 OR $hours > 0)
{
if ($hours > 0)
{
$str .= $hours.' '.$CI->lang->line((($hours > 1) ? 'date_hours' : 'date_hour')).', ';
}
$seconds -= $hours * 3600;
}
// don't display minutes/seconds unless $display_mins_secs
// == true
if ($display_mins_secs)
{
$minutes = floor($seconds / 60);
if ($days > 0 OR $hours > 0 OR $minutes > 0)
{
if ($minutes > 0)
{
$str .= $minutes.' '.$CI->lang->line((($minutes > 1) ? 'date_minutes' : 'date_minute')).', ';
}
$seconds -= $minutes * 60;
}
if ($str == '')
{
$str .= $seconds.' '.$CI->lang->line((($seconds > 1) ? 'date_seconds' : 'date_second')).', ';
}
}
return substr(trim($str), 0, -1);
}
?>
CodeIgniter will automagically find this file and use your timespan() function instead of the built-in version. The "Extending Helpers" section of this page offers a little more information about how this works.
To take advantage of the "extended" timespan() function, just pass a third argument of false, like this:
$variable = timespan($secs, $time, false);
There's possibly a more elegant solution, maybe using PHP's native DateTime object (see here), but from the question I think the above timespan() modification more closely fits your needs.
I know this is an old thread but I just found it and was almost the solution for what I was looking for. I just wanted to have a level limit by number. I did a small modification in the script where I make the $str as an array and add each year, month, day etc. Then I walk through the array until the given limit. After that I just implode a ", " and return the result.
Maybe someone else also can enjoy this script?
<?php
// Adds a third argument to timespan() that limits the text string at given level
function timespan($seconds = 1, $time = '', $limit=null)
{
$CI =& get_instance();
$CI->lang->load('date');
if ( ! is_numeric($seconds))
{
$seconds = 1;
}
if ( ! is_numeric($time))
{
$time = time();
}
if ($time <= $seconds)
{
$seconds = 1;
}
else
{
$seconds = $time - $seconds;
}
$str = array();
$years = floor($seconds / 31536000);
if ($years > 0)
{
$str[] = $years.' '.$CI->lang->line((($years > 1) ? 'date_years' : 'date_year'));
}
$seconds -= $years * 31536000;
$months = floor($seconds / 2628000);
if ($years > 0 OR $months > 0)
{
if ($months > 0)
{
$str[] = $months.' '.$CI->lang->line((($months > 1) ? 'date_months' : 'date_month'));
}
$seconds -= $months * 2628000;
}
$weeks = floor($seconds / 604800);
if ($years > 0 OR $months > 0 OR $weeks > 0)
{
if ($weeks > 0)
{
$str[] = $weeks.' '.$CI->lang->line((($weeks > 1) ? 'date_weeks' : 'date_week'));
}
$seconds -= $weeks * 604800;
}
$days = floor($seconds / 86400);
if ($months > 0 OR $weeks > 0 OR $days > 0)
{
if ($days > 0)
{
$str[] = $days.' '.$CI->lang->line((($days > 1) ? 'date_days' : 'date_day'));
}
$seconds -= $days * 86400;
}
$hours = floor($seconds / 3600);
if ($days > 0 OR $hours > 0)
{
if ($hours > 0)
{
$str[] = $hours.' '.$CI->lang->line((($hours > 1) ? 'date_hours' : 'date_hour'));
}
$seconds -= $hours * 3600;
}
$minutes = floor($seconds / 60);
if ($days > 0 OR $hours > 0 OR $minutes > 0)
{
if ($minutes > 0)
{
$str[] = $minutes.' '.$CI->lang->line((($minutes > 1) ? 'date_minutes' : 'date_minute'));
}
$seconds -= $minutes * 60;
}
if ($str == '')
{
$str[] = $seconds.' '.$CI->lang->line((($seconds > 1) ? 'date_seconds' : 'date_second'));
}
// Only show number of array elements depending on limit.
if($limit)
{
$i = 0;
foreach($str as $level)
{
if($i >= $limit)
unset($str[$i]);
$i++;
}
}
$return = implode(', ', $str);
return trim($return);
}
i have created work total time program in PHP - give input time - 1.30, 2.10, 1.40 and get output time - 4.80(8 hrs). but i need output time - 5.20(8.40 hrs).
Notes: 1.30+2.10+1.40=4.80(8 hrs), but i need 5.20(8.40 hrs). please help me...
1.30 + 2.10 + 1.40 is wrong. Should be:
((1 * 60) + 30) + ((2 * 60) + 10) + ((1 * 60) + 40) = 320 (minutes)
320 minutes = 5 hours and 20 minutes.
You need to keep track of minutes and seconds separately:
$minutes = array();
$seconds = array();
foreach ($times as $time) {
$parts = explode('.', $time);
$minutes[] = $time[0];
$seconds[] = $time[1];
}
$total_minutes = array_sum($minutes);
$total_seconds = array_sum($seconds);
while ($total_seconds > 60) {
$total_minutes++;
$total_seconds -= 60;
}
echo $total_minutes . ' minutes and ' . $total_seconds . ' seconds';
Excerpt from PHP site for your pleasure:
function AddTime ($oldTime, $TimeToAdd) {
$pieces = split(':', $oldTime);
$hours=$pieces[0];
$hours=str_replace("00","12",$hours);
$minutes=$pieces[1];
$seconds=$pieces[2];
$oldTime=$hours.":".$minutes.":".$seconds;
$pieces = split(':', $TimeToAdd);
$hours=$pieces[0];
$hours=str_replace("00","12",$hours);
$minutes=$pieces[1];
$seconds=$pieces[2];
$str = $minutes." minute ".$seconds." second" ;
$str = "01/01/2000 ".$oldTime." am + ".$hours." hour ".$minutes." minute ".$seconds." second" ;
if (($timestamp = strtotime($str)) === false) {
return false;
} else {
$sum = date('h:i:s', $timestamp);
$pieces = split(':', $sum);
$hours = $pieces[0];
$hours = str_replace("12", "00", $hours);
$minutes = $pieces[1];
$seconds = $pieces[2];
$sum = $hours.":".$minutes.":".$seconds;
return $sum;
}
}
$firstTime = "00:03:12";
$secondTime = "02:04:34";
$sum=AddTime($firstTime, $secondTime);
if($sum != false) {
echo $firstTime." + ".$secondTime." = ".$sum;
} else {
echo "failed";
}
Output:
00:03:12 + 02:04:34 = 02:07:46
For each number (represented as $t below) you can do this:
// start with $total=0
$hours = floor($t); // 1.10 -> 1 hr
$minutes = ($t - $hours) * 100; // 1.10 -> 10 mins
$total += ($hours * 60) + $minutes;
That gives you the total number of minutes. To get hours/mins separately, do this:
$total_mins = $total % 60; // 130 -> 10 mins
$total_hours = ($total - $total_mins) / 60; // 130 -> 2 hrs