简体   繁体   English

php基于当前日期倒计时?

[英]php countdown based on the current date?

I want to make a countdown, which counts to the next first friday in a month. 我要进行倒计时,这要算到一个月的下一个星期五。 So if we have the 1st august the next friday is the 3rd august, but if we have the 5th august the next friday would be the September 7th. 因此,如果我们有8月1日,那么下一个星期五是8月3日,但是如果我们有8月5日,那么下一个星期五是9月7日。

<?php
    $help=strtotime(date('Y')+date('m')+'-00 next friday');
    if (time()*1000 > $help* 1000+(18*60*60*1000)) {
        $count = strtotime(date('Y')+'-'+date('m')+'-00 next friday') * 1000+(18*60*60*1000);
        //case month is passed i need the next month 
    }
    else {
        $count = strtotime(date('Y')+'-'+date('m')+'-00 next friday') * 1000+(18*60*60*1000);
         //case actual month first friday isn't passed need current month
    }
?>

That is the code i actual have, but there are mistakes in the 4th and in the 8th line. 那是我实际拥有的代码,但是在第四行和第八行中有错误。 Please help me. 请帮我。 Thank you 谢谢

Given a valid date you can proceed like this to know the next Friday: 给定一个有效的日期,您可以像下面这样继续下一个星期五:

    $date= '2018-08-31 06:30';
    $strtotime=strtotime($date);
    $start=date_create($date);
    do{ 
        $strtotime=strtotime($start->add( date_interval_create_from_date_string('1 day'))->format('Y-m-d H:i:s'));
    }while(date('l',$strtotime)!='Friday');

print($start->format('Y-m-d H:i:s'));

the output will be : 输出将是:

2018-09-07 06:30:00

for the countdown you can proceed like this given the same date or whatever you want (this must follow the code above): 对于倒计时,您可以在给定日期或您想要的任何条件下像这样进行倒计时(必须遵循上面的代码):

$start->setTime(0,0);
$countdown = date_diff($start,date_create($date));
$days = $countdown->d>1?'days':'day';
$hours = $countdown->h>1?'hours':'hour';
$minutes = $countdown->i>1?'minutes':'minute';
$countdown = $countdown->format("%D $days %H $hours %I $minutes");
print_r($countdown);

the output is : 输出为:

06 days 17 hours 30 minutes

Finally based on explanations above your full script for the countdown could be: 最后,根据上面的解释,您的倒计时完整脚本可能是:

$date= date('Y-m-d H:i:s');//you can put any valid date here
$date_prime=$date;
$strtotime=strtotime($date);
$start=date_create($date);
$start->setDate((int)date('Y',$strtotime),(int)date('m',$strtotime),1);
$start->setTime(0,0);
do{
    $strtotime=strtotime($start->add( date_interval_create_from_date_string('1 day'))->format('Y-m-d H:i:s'));
}while(date('l',$strtotime)!='Friday');
$readable_format=$start->format('Y-m-d H:i:s');
if(strtotime($readable_format)>$strtotime){
    $countdown=date_diff($start,date_create($date_prime));
    $days=$countdown->d>1?'days':'day';
    $hours=$countdown->h>1?'hours':'hour';
    $minutes=$countdown->i>1?'minutes':'minute';
    $countdown=$countdown->format("%D $days %H $hours %I $minutes");
}
else{
    $start->setDate((int)date('Y',$strtotime),(int)date('m',$strtotime),(int)date('t',$strtotime));
    do{
        $strtotime=strtotime($start->add( date_interval_create_from_date_string('1 day'))->format('Y-m-d H:i:s'));
    }while(date('l',$strtotime)!='Friday');
    $readable_format=$start->format('Y-m-d H:i:s');
    $countdown=date_diff($start,date_create($date_prime));
    $days=$countdown->d>1?'days':'day';
    $hours=$countdown->h>1?'hours':'hour';
    $minutes=$countdown->i>1?'minutes':'minute';
    $countdown=$countdown->format("%D $days %H $hours %I $minutes");
}
print_r($countdown); will actually give:
24 days 23 hours 55 minutes

If you're just trying to countdown to an event use this code and adapt it to your needs 如果您只是想倒计时某个事件,请使用此代码并使其适应您的需求

// Todays date
$today = time();

// Day you want to reach
$event = mktime(0,0,0,12,25,2018);

// Calculates days until event
$countdown = round(($event - $today)/86400);

echo "$countdown days until whatever";

?> ?>

Ok, I found the solution. 好的,我找到了解决方案。

 <?php
    $help=strtotime("first friday of this month");
    if (time()*1000 > $help* 1000+(18*60*60*1000)) {
        $count = strtotime("first friday of next month") * 1000+(18*60*60*1000);
    }
    else {
        $count =  strtotime('next friday')* 1000+(18*60*60*1000);
    }
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM