Select all the dates between two chosen dates [duplicate] - php

This question already has answers here:
PHP: Return all dates between two dates in an array [duplicate]
(26 answers)
Closed 8 years ago.
I am building an app on php,mysql which is related to events. This is a specific feature where the user will repeat an activity/event . The user will select a start date and an end date . Now i am trying to figure out how to calculate the dates between those two selected dates. I am using jquery's datepicker for date selection .
$start_date = date("Y-m-d", strtotime($_POST['start_date']));
$end_date = date("Y-m-d", strtotime($_POST['end_date']));
Datediff doesn't seem to do the work as it only gives the difference between two dates . I want all the dates between those two dates .
Would be great if someone could advice as per how to go about this .

By this way You can fetch the dates between two dates
$start_date = strtotime($_POST['start_date']);
$end_date = strtotime($_POST['end_date']);
// Loop between timestamps, 24 hours at a time
for ( $i = $start_date ; $i <= $end_date ; $i = $i + 86400 ) {
echo date( 'Y-m-d', $i )."<br>";
}

function createDateRangeArray($strDateFrom,$strDateTo)
{
$aryRange=array();
$iDateFrom=mktime(1,0,0,substr($strDateFrom,5,2), substr($strDateFrom,8,2),substr($strDateFrom,0,4));
$iDateTo=mktime(1,0,0,substr($strDateTo,5,2), substr($strDateTo,8,2),substr($strDateTo,0,4));
if ($iDateTo>=$iDateFrom)
{
array_push($aryRange,date('Y-m-d',$iDateFrom)); // first entry
while ($iDateFrom<$iDateTo)
{
$iDateFrom+=86400; // add 24 hours
array_push($aryRange,date('Y-m-d',$iDateFrom));
}
}
return $aryRange;
}
Found the above solution and it works great. Thanks everyone for the time and advice.

I would recommend you to use DateTime objects
date_default_timezone_set("Europe/Istanbul");
$dt = new DateTime("10-01-2014");
$diff = $dt->diff(new Datetime("12-01-2014"));
for($i = 1; $i <= $diff->days; $i++) {
$nextdate = $dt->modify("+1 day");
echo $nextdate->format("d-m-Y"); echo "\n";
}

Related

count occurrence of date (e.g 14th) between two dates

How can I count occurrences of 14th of a month between two dates
For example between 07.05.2018 and 04.07.2018
I have 2 occurrences of the 14th
Try this. Note that I've changed your date format, but you can just do a createFromFormat if you're really keen on your own format.
$startDate = new DateTime('2018-05-07');
$endDate = new DateTime('2018-07-04');
$dateInterval = new DateInterval('P1D');
$datePeriod = new DatePeriod($startDate, $dateInterval, $endDate);
$fourteenths = [];
foreach ($datePeriod as $dt) {
if ($dt->format('d') == '14') { // Note this is loosely checked!
$fourteenths[] = $dt->format('Y-m-d');
}
}
echo count($fourteenths) . PHP_EOL;
var_dump($fourteenths);
See it in action here: https://3v4l.org/vPZZ0
EDIT
This is probably not an optimal solution as you loop through every day in the date period and check whether it's the fourteenth. Probably easier is to modify the start date up to the next 14th and then check with an interval of P1M.
You don't need to loop at all.
Here's a solution that does not loop at all and uses the less memory and performance hungry date opposed to DateTime.
$start = "2018-05-07";
$end = "2018-07-04";
$times = 0;
// Check if first and last month in the range has a 14th.
if(date("d", strtotime($start)) <= 14) $times++;
if(date("d", strtotime($end)) >= 14) $times++;
// Create an array with the months between start and end
$months = range(strtotime($start . "+1 month"), strtotime($end . "-1 month"), 86400*30);
// Add the count of the months
$times += count($months);
echo $times; // 2
https://3v4l.org/RevLg

Increment days in PHP MySQL date [duplicate]

This question already has answers here:
Adding days to $Date in PHP
(12 answers)
Closed 5 years ago.
I have a table in MySQL with a date field (called NDate) which contains standard date values ("2017-04-17","2017-04-18", etc.).
Through PHP webpage, I am trying to take the system date (say today is 2017-04-17), and then pull all rows from the above table where NDate="2017-04-17". No issues till here.
I have a requirement to increment the day (starting today and going on for next 10 days - i.e. 2017-04-17 to 2017-04-26), and for each day report entries under a different heading like "Entries for 2017-04-17" which will list all rows having NDate 2017-04-17, "Entries for 2017-04-18" which will list all rows having NDate 2017-04-18.
I was trying to use a for loop with PHP date_modify function to increment the days one by one, but it is not showing any results.
Here are the selected pieces of code:
date_default_timezone_set('US/Eastern');
$datev = date("Y-m-d");
for ($x = 0; $x <= 10; $x++)
{
$datev=date_modify($date,"+$x days");
echo "before date format<br>"; // echo statement 1
echo "date is: $datev <br>"; // echo statement 2
$sql = "SELECT * FROM tablename where Ndate='$datev'";
echo "before result<br>"; // echo statement 3
...
...
...
}
Output on webpage shows only statement 1. But echo stats 2 and 3 are not printed.
You can increment days using strtotime function as a parameter to date function.
For 10 days, you can use for loop, to build an array of days. Then iterate over it, to execute queries you need.
$today = date('Y-m-d');
$dates=array($today);
for($i=1;$i<10;$i++) {
$NewDate=date('Y-m-d', strtotime("+".$i." days"));
$dates[]=$NewDate;
}
foreach($dates as $dt) {
// sql stuff here
echo "date is: $dt <br>";
$sql = "SELECT * FROM tablename where Ndate='$dt'";
echo "before result<br>";
// .....
}
This code should work for your case. If any problems, just let me know.
Try this:
$start = strtotime(date('Y-m-d'));
$end = strtotime(date('Y-m-d', strtotime('+10 days')));
while($start <= $end)
{
$date = date('Y-m-d', $start);
//use $date to do stuff
//SELECT * FROM tablename where Ndate='$date'
$start = strtotime("+1 day", $start);
}

Converting day of week to dates for the current month with MYSQL & PHP

Spent a while searching and couldn't find any answers. Really simple question I hope.
In my database I have a string column dayOfWeek.
I use this to track what day of week weekly events are scheduled for. Usually I match the dayOfWeek to the current day using PHP and display the correct events.
What I want to do is show a calendar into the future which makes a list of the future dates of the weekly recurring event.
For example, if the event takes place on Tuesday, and Feb 2017 has Tuesdays on the 7th, 14th, 21st, and 28th. I'd like to use PHP to display as such.
I know strtotime() is used to do the opposite, find a day from the date, but can the opposite be done easily?
Sorry if my question is poorly worded or missing information.
USE CASE FOR FEB 2017 TUESDAYS
$n=date('t',strtotime("2017-02-01")); // find no of days in this month
$dates=array();
for ($i=1;$i<=$n;$i++){
$day=date("D",strtotime("2017-02-".$i)); //find weekdays
if($day=="Tue"){
$dates[]="2017-02-".$i;
}
}
print_r($dates);
http://phpfiddle.org/main/code/s8wq-xx44
You could do this:
// The day of the week.
$dayOfEvent = row['day'];
// Say, you wanted the next 5 weeks.
$numberOfWeeks = 5;
$tempDate = date(U);
$counter = 0;
do{
$tempDate = $tempDate + 86,164;
if(date('D', $tempDay) == 'Tue'){
echo date('l jS \of F Y', $tempDay);
$counter++;
}
}while($counter < $numberOfWeeks);
First try to find the date for the day in current week. After that keep adding 7 days multiples to it.
<?php
// $timestamp = now();
for ($i=0; $i < 7; $i++) {
$days = $i." days";
$t = date('Y-m-d', strtotime($days));
$dayOfTheDay = date("D",strtotime($t));
if ( $dayOfTheDay == day from table )
{
$current_date = $t;
break;
}
}
for ($i=0; $i < 200; $i++) {
in this loop add 7 days to current_date
}
?>

Get the day number from a date [duplicate]

This question already has answers here:
PHP - How to get year, month, day from time string
(5 answers)
Closed 7 years ago.
I want to get the day number of a date.
For example my date is: 2015-12-31, it should return 365 and 2015-01-1 would be 1.
How can I do that?
EDIT: date('z') does not work for me, I dont want the current date
Use strtotime() — to parse about any English textual datetime description into a Unix timestamp. And getdate() — Get date/time information.
<?php
$day = getdate(strtotime("2015-12-31"));
echo $day['yday']+1; // output 365
?>
Try
<?php
$today = getdate(strtotime('2015-12-31'));
echo '<pre>';
print_r($today['yday'] + 1);
?>
Read GetDate
You can try it.
<?php
$day = 31;
$mon = 12;
$year = 2015;
$sum = 0;
$days = 0;
$month_day = array(31,28,31,30,31,30,31,31,30,31,30,31);
for ( $i = 0; $i < $mon; $i++){
$sum += $month_day[$i];
}
$days = $sum - ($month_day[$mon-1] - $day);
echo "$days";
?>
You may get day, mon, year from your expected date and make it integer. You should check the year is leap year or not. It may vary one day if it is a leap year. I think you shall do it.

Generate Friday Cutoff on PHP or MySQL [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Finding date range for current week, month and year
Can Anyone help me on how could I generate Friday cutoff using PHP or MySQL. below is the sample date range
...
2012-10-06 to 2012-10-12
2012-10-13 to 2012-10-19
2012-10-20 to 2012-10-26
...
2012-12-22 to 2012-12-28
2012-12-29 to 2013-01-04
Knowing the first saturday (the initial period) is easy to calculate the next friday (and looping the next saturday, next friday of second saturday....)
Check this code:
<?php
$saturday = '2012-10-06';
$next_friday = date('Y-m-d', strtotime('+6 days', strtotime($saturday)));
print "The next friday is $next_friday";
Try this code ...
function getCurrentWeek($start , $end){
$ts = strtotime($start);
$te = strtotime($end);
$days = round(abs($te-$ts)/86400);
$range = '';
$j = 0;
// generate dates
for ($i=0; $i<$days; $i++, $ts+=86400){
$temp_date = date("Y-m-d", $ts);
$d = (int) date('w',$ts);
switch ($d) {
case 5 :
if($j == 1)
$range .= $temp_date . "<br />" ;
$j = 0;
break;
case 6 :
$j = 1;
$range .= $temp_date . " TO " ;
break;
}
}
return $range;
}
echo getCurrentWeek('2012-10-06','2013-01-06');
Ps) You need to input correct start and end date where you have a Friday at the end ... or you will have XXXX-XX-XX TO EMPTY

Categories