开发者

Get this weekend in php?

开发者 https://www.devze.com 2023-01-26 11:10 出处:网络
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());

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 function is magic, most of people don't know but you can do a lot of things with this function.

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


Use strtotime()

$sat = strtotime('saturday');


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


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" );
}


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");
?>


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; 
0

精彩评论

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