Top Ad unit 728 × 90

How to get all Sundays between two dates using PHP

How to get all Sundays between two dates using PHP. Say, you want to automatic generated delivery date after creating a bill date. Assume that you have already defined no of delivery days of a particular product saved in the database.

So when you add a particular product in a bill then it will generate a delivery date automatically based on no of delivery days and if it founds sundays between two dates (Bill date and delivery date) then following code calculates how many sundays are there between two dates.

$No_of_Days = trim($row2['Delivery_Days']); // get no of delivery days from database
//***********Start it will count all sundays***********
date_default_timezone_set('Asia/Kolkata'); //you can set timezone according to your area
//$current_date = date("Y-m-d"); //to get the current date as 2017-09-09
//Delivery date has been calculated with bill date not current date..
//if you need to be calculated by current date then just replace bill_date with current_date...

$Bill_Date = asenDMYtoYMD($Bill_Date); //asenDMYtoYMD is customized function to change the date format from dd/mm/yyyy to yyyy-mm-dd
$GetToDate = Date($Bill_Date, strtotime("+$No_of_Days days"));

$from_date = $Bill_Dt; //yyyy-mm-dd
$to_date = $GetToDate; //yyyy-mm-dd
$AllSunDays_Between_2Dates = 0; // assigned a variable to zero
$startdate = strtotime("sunday".$from_date);
$enddate = strtotime($to_date);
$from_date = strtotime($from_date);
$to_date = strtotime($to_date);
while($startdate <= $enddate)
{
      //echo date("Y-m-d", $startdate) . "<br>"; //it will show sunday's date
      $startdate = strtotime("+1 week", $startdate);
      $AllSunDays_Between_2Dates++;
}
//echo "AllSunDays_Between_2Dates = ". $AllSunDays_Between_2Dates."<br><br>";
$Actual_No_of_Delivery_Days = intval($No_of_Days) + intval($AllSunDays_Between_2Dates); //echo "Actual_No_of_Delivery_Days = ".$Actual_No_of_Delivery_Days."<br>";
//***********End it will count all sundays***********
//---------------------------
//adding Actual_No_of_Delivery_Days with current dates and it shows date
$Actual_Delivery_Date = date ("d/m/Y", strtotime ($Bill_Dt ."+".$Actual_No_of_Delivery_Days." days"));


If you have any queries regarding this please give a comment I will definitely  try to help you.
How to get all Sundays between two dates using PHP Reviewed by Ashok Sen on 14:57:00 Rating: 5

No comments:

Website Design Company at Kolkata All Rights Reserved © 2014 - 2019
Developed by Asenwebmedia

Contact Form

Name

Email *

Message *

Powered by Blogger.