开发者

php to count the remaining time frame

i am building a quiz system,

the fields on the table is:

id, title, description, status, created, ended

the logic goes this way:

a quiz last only one week, it will automatically ends on the last day of the week (i am thinking on Saturday 24:00), and the admin should input the quiz on any day of the week, if it is inputted on Wednesday say at 01:00 pm, then the system should count the remaining time frame (in this case it would be around 8开发者_高级运维3 hours) and then adds it to current time() to be inserted to the "ended" field.

am i on the right track?? and if i am, how should i count the remaining time frame in PHP ?

thanks


You can get the time stamp using strtotime(); and use the argument 'next saturday', so you could calculate the time remaining by doing something like this:

$timeRemaining = strtotime('next saturday') - time();

That will give you a timestamp in seconds which you can divide by 3600 to get the hours.


On PHP >= 5.3.0 you can use the function date_diff

On previous version you can use something like this

$date1 = time(); 
$date2 = mktime(0,0,0,01,28,2011);

$diff = $date2 - $date1;


You can find the ending time by using the mktime or strtotime function, and find the time remaining using

// Saturday, this week
$ends = strtotime('next saturday');
$remaining = $ends - time(); // Remaining time in seconds


If I have understood the question correctly, i.e. you want the end date to be stored in the db then Can this not just be achieved with

$ended = strtotime("+1 week");


Note that 24:00 on Saturday doesn't exist. The time goes from 23:59:59 on Saturday to 00:00:00 on Sunday.

No need to add the times together manually. When the quiz is created you can use $end_time = strtotime( 'next Sunday' ); to get the time for the quiz to end (00:00:00 on Sunday morning).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜