Jump to content

Recommended Posts

hi guys if you want to help me out a little here.

 

I think i screwed up the math somewhere but i'm trying to create a countdown that starts at 1 hour and keeps going lower and lower until it hits 0 hours 0 minutes 0 seconds then next refresh of the page should reset the countdown back to 1 hour (this is to get people excited when they shop on my shop)

 

<?php
  $timeleft = 0;
  $file = fopen("happyhour.txt", "r");
  $checkedtime = fgets($file);
  fclose($file);
  if(time() - $checkedtime >=  3600) {
    $file = fopen("happyhour.txt", "w");
    $timeleft = time();
    fputs($file, $timeleft);
    fclose($file);
  } else {
    $timeleft = 3600 - (time() - $checkedtime);
  }

echo "var hour = ".date("g", $timeleft).";";
echo "var min = ".date("i", $timeleft).";";
echo "var sec = ".date("s", $timeleft).";";
?>

 

where did i screw up? seems to keep outputting around 7 hours? wth!

 

thanks guys

Link to comment
https://forums.phpfreaks.com/topic/204115-hour-countdown-restart-after-000/
Share on other sites

are you trying reload the page every hour?  if so, you need to use a client-side script such as javascript, not PHP which runs server side.  Your script will load once and then output the static text to the browser and nothing else will ever happen unless some script running in the users browser (not on your server) tells the browser to reload the page.

no no i'm doing counter count down synchronization with javascript using php.

 

when the javascript counter hits 0 clientside should be same time if not a second off in php so when I'd refresh the page the countdown should start at a hour again serversided in php.

excuse me? you think i don't know how to use google?

 

I'm asking for help in fixing my code not rediections to different websites.. I've checked couldn't find what i wanted.

 

Okay lets just forget about javascript okay?? just when i refresh page i want it to update the countdown hours minutes seconds.

 

when the clock hits 0 hours 0 minutes 0 seconds.

 

it should re-save a new timestamp.

 

Thank you

Sorry, I haven't had a chance to use HLMGTFY in the last few days, so I jumped on it. :P

 

Example:

<?php
session_start();
$timestamp = time();
$diff = 3600;
if(isset($_SESSION['ts'])) {
$slice = ($timestamp - $_SESSION['ts']);	
$diff = $diff - $slice;
}

if(!isset($_SESSION['ts']) || $diff > 3600 || $diff < 0) {
$diff = 3600;
$_SESSION['ts'] = $timestamp;
}

//Below is demonstration of output.  Seconds could be passed to Javascript.
$hours = $diff; //$diff holds seconds less than 3600 (1 hour);

echo floor($hours / 3600) . ' : ';
$minutes = $hours % 3600;
echo floor($minutes / 60) . ' : ';
$seconds = $minutes % 60;
echo floor($seconds);
?>

 

 

Since I was being a *(&# I decided to go ahead and make something up for you.

 

 

It works, but you may want to change the formatting of it.

 

<?php
session_start();
$timestamp = time();
$diff = 3600;
if(isset($_SESSION['ts'])) {
$slice = ($timestamp - $_SESSION['ts']);	
$diff = $diff - $slice;
}

if(!isset($_SESSION['ts']) || $diff > 3600 || $diff < 0) {
$diff = 3600;
$_SESSION['ts'] = $timestamp;
}

//Below is demonstration of output.  Seconds could be passed to Javascript.
$diff; //$diff holds seconds less than 3600 (1 hour);

$hours = floor($diff / 3600) . ' : ';
$diff = $diff % 3600;
$minutes = floor($diff / 60) . ' : ';
$diff = $diff % 60;
$seconds = $diff;


?>
<div id="strclock">Clock Here!</div>
<script type="text/javascript">
var hour = <?php echo floor($hours); ?>;
var min = <?php echo floor($minutes); ?>;
var sec = <?php echo floor($seconds); ?>

function countdown() {
if(sec <= 0 && min > 0) {
  sec = 59;
  min -= 1;
}
else if(min <= 0 && sec <= 0) {
  min = 0;
  sec = 0;
}
else {
  sec -= 1;
}

if(min <= 0 && hour > 0) {
  min = 59;
  hour -= 1;
}

var pat = /^[0-9]{1}$/;
sec = (pat.test(sec) == true) ? '0'+sec : sec;
min = (pat.test(min) == true) ? '0'+min : min;
hour = (pat.test(hour) == true) ? '0'+hour : hour;

document.getElementById('strclock').innerHTML = hour+":"+min+":"+sec;
setTimeout("countdown()",1000);
}
countdown();
</script>

lol its okay man wow u put alot of work into it there was nothing wrong with my javascript lol its just the php countdown.

 

now it's well session based wont sync up with every person thats why i made it load timestamp from a file and i just wanted someone to correct my code why it gave bad readings

solved thanks to you man

seems date() fails on 3600 shows it as 7 hours rofl.. but my old server shown it as 1 hour strange but yah..

 

<?php
  $timeleft = 0;
  @$file = fopen("happyhour.txt", "r");
  $checkedtime = @fgets($file);
  @fclose($file);
  if((time() - $checkedtime >  3600) || time() - $checkedtime < 0) {
    $file = fopen("happyhour.txt", "w");
    $checkedtime = time();
    fputs($file, $checkedtime);
    fclose($file);
  }
  $timeleft = 3600 - (time() - $checkedtime);
  $hours = floor($timeleft/3600);
  $minutes = floor(($timeleft%3600)/60);
  $seconds=floor($timeleft%60);

echo "var hour = ".$hours.";";
echo "var min = ".$minutes.";";
echo "var sec = ".$seconds.";";
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.