Get Current Months And Week Start To End Dates in PHP
Usage : -
Sometimes in your Program We Need show the data according to calendar type like current week data or Current month only or prevoius month then for that condition we use this logic to fetch data from database according to date.
For Months : -
<?php
$date=strtotime(date("Y-m-d H:i:s"));
$month=date('m',$date);
$year=date('Y',$date);
$days=date('t',$date);
$sdates=$year."-".$month."-01 00:00:00";
$edates=$year."-".$month."-".$days." 23:59:59";
echo "Start Date ".$sdates." end dates".$edates;
?>
$date=strtotime(date("Y-m-d H:i:s"));
$month=date('m',$date);
$year=date('Y',$date);
$days=date('t',$date);
$sdates=$year."-".$month."-01 00:00:00";
$edates=$year."-".$month."-".$days." 23:59:59";
echo "Start Date ".$sdates." end dates".$edates;
?>
For Weeks : -
<?php
$monday = strtotime("last monday");
$monday = date('w', $monday)==date('w') ? $monday+7*86400 : $monday;
$sunday = strtotime(date("Y-m-d",$monday)." +6 days");
$this_week_sd = date("Y-m-d",$monday);
$this_week_ed = date("Y-m-d",$sunday);
echo "Current week range from $this_week_sd to $this_week_ed ";
?>
$monday = strtotime("last monday");
$monday = date('w', $monday)==date('w') ? $monday+7*86400 : $monday;
$sunday = strtotime(date("Y-m-d",$monday)." +6 days");
$this_week_sd = date("Y-m-d",$monday);
$this_week_ed = date("Y-m-d",$sunday);
echo "Current week range from $this_week_sd to $this_week_ed ";
?>
No comments:
Post a Comment