Get this weekend in php?

I am 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 can I get the current weekend depending on the current date? Thank!

+3
source share
6 answers

PHP strtotime function is magic, most people do not know, but you can do a lot with this function.

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

Use strtotime()

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

:

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

"" - , . PHP

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

-

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

Source: https://habr.com/ru/post/1776173/


All Articles