PHP server side timer

自闭症网瘾萝莉.ら 提交于 2019-11-29 12:56:09
x29a

Judging from "when ever a user opens the page" there should not be an auto-update mechanism of the page? If this is not what you meant, look into AJAX (as mentioned in the comments) or more simply the HTML META refresh. Alternatively, use PHP and the header()

http://de2.php.net/manual/en/function.header.php

method, described also here:

Refresh a page using PHP

For the counter itself, you would need to save the end date (e.g. a database or a file) and then compare the current timestamp with the saved value.

Lets assume there is a file in the folder of your script containing a unix timestamp, you could do the following:

<?php
$timer = 60*5; // seconds
$timestamp_file = 'end_timestamp.txt';
if(!file_exists($timestamp_file))
{
  file_put_contents($timestamp_file, time()+$timer);
}
$end_timestamp = file_get_contents($timestamp_file);
$current_timestamp = time();
$difference = $end_timestamp - $current_timestamp;

if($difference <= 0)
{
  echo 'time is up, BOOOOOOM';
  // execute your function here
  // reset timer by writing new timestamp into file
  file_put_contents($timestamp_file, time()+$timer);
}
else
{
  echo $difference.'s left...';
}
?>

You can use http://www.unixtimestamp.com/index.php to get familiar with the Unix Timestamp.

There are many ways that lead to rome, this is just one of the simple ones.

you can use Cron Jobs Ex: Scheduling a Job For a Specific Time 30 08 10 06 * /home/sendtouser.php 30 – 30th Minute 08 – 08 AM 10 – 10th Day 06 – 6th Month (June) * – Every day of the week

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!