简体   繁体   中英

Get this weekend in php?

I'm trying to get this weekend in php.

I get the current date by code :

$start_date = date("Y-m-d H:i:s", time());

How I can get the current weekend depend on the curent date? Thank you!

PHP strtotime功能很神奇,大多数人都不知道但是你可以用这个功能做很多事情。

$start_date = date("Y-m-d H:i:s", strtotime('next Saturday'));

使用strtotime()

$sat = strtotime('saturday');
$start_date = date("Y-m-d H:i:s", strtotime('Saturday'));

First weekend calculation

if(date('w')==6){// Today is Saturday | Bugün Cumartesi ise
  $wknd=date("Y-md-").'-'.date("Y-m-d", strtotime('next Sunday'));
}if(date('w')==0){// Today is Sunday | Bugün Pazar ise
  $wknd=date("Y-m-d");
}else{ // On weekdays | Hafta içi ise
 $wknd=date("Y-m-d", strtotime('next Saturday')).'-'.date("Ymd", strtotime('next Sunday'));
}
echo $wknd; 

Try this:

<?php
$time = mktime();
$found = false;
while(!$found) {
    $d = date('N', $time);
    if($d == 6 || $d == 7) {
        $found = true;
        $weekend = date('d/m/Y G:i:s', $time);
    }
    $time += 86400;
}
echo("Weekend begins on: $weekend");
?>

My definition of "weekend" is next Friday, end of office hours up to end of Sunday. In PHP, this would be

$weekend = new DatePeriod(
    new DateTime('next Friday 18:00'),
    DateInterval::createFromDateString('+1 hour'),
    new DateTime('next Friday +2 day 23:59'));

foreach($weekend as $hours) {
    echo $hours->format( "l Y-m-d H:i:s\n" );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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