PHP - Fetch all dates between two dynamic UNIX timestamps [duplicate] - php

This question already has answers here:
PHP: Return all dates between two dates in an array [duplicate]
(26 answers)
Closed 4 years ago.
I want to get all the dates between two given specific dates which will be changing dynamically according to the user requirements.
This works fine for me :
$begin = new DateTime( '2018-07-01' );
$end = new DateTime( '2018-08-10' );
$end = $end->modify( '+1 day' );
$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval ,$end);
$dates = [];
foreach($daterange as $date){
array_push($dates,$date->format('Y-m-d'));
}
return $dates;
But this gives me NULL array when I insert dates dynamically. For example,
$begin = new DateTime($request->date1);
$end = new DateTime(today());
$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval ,$end);
$dates = [];
foreach($daterange as $date){
array_push($dates,$date->format('Y-m-d'));
}
return dates;
I have tried many inputs like storing today's date in a variable, changing it's format and then using that variable as an input. I tried changing date variable to string using (string) function and also I have used Carbon classes for dates, but somehow I am making mistake using DateTime, DateInterval and DatePeriod as I don't know the basics of them.
I need to get all the dates between two specific dates for charts in my admin panel.

Finally got the solution for this question. It worked by converting the dates to UNIX timestamp. Here's the code, how it worked:
$date_from = "2018-07-01";
$date_from = strtotime($date_from); // Convert date to a UNIX timestamp
// Specify the end date. This date can be any English textual format
$date_to = strtotime(today()); // Convert date to a UNIX timestamp
$dates = [];
// Loop from the start date to end date and output all dates inbetween
for ($i=$date_from; $i<=$date_to; $i+=86400) {
array_push($dates,date("Y-m-d", $i));
}
return $dates;
Here is the reference where I found this solution, hope this helps others too.

Related

How to hide saturday and sunday in PHP array [duplicate]

This question already has answers here:
Get date range between two dates excluding weekends
(4 answers)
Closed 2 years ago.
I want to hide Saturday and Sunday in PHP.
I´ve build the following code:
$begin = new DateTime($row['date']);
$end = new DateTime($row['dateul']);
$end = $end->modify( '+1 day' );
$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval ,$end);
foreach($daterange as $date){
$array[] = $date->format("Y-m-d");
}
Until here the code is working but it outputs the complete week/days in this daterange.
I found this code:
if (strcasecmp($daterange, 'Sun') != 0
&& strcasecmp($daterange, 'Sat') != 0){
}
Do I understand it right, that if value = 1 it will output Saturday for example?
Because the main idea was the following:
if day for example sunday = 0 hide it in array, if sunday=1 show it in array.
The values are coming from MySQL.
You can use the N format for DateTime::format to check the day of week, it returns 6 for Saturday and 7 for Sunday, so as long as the value is less than 6, add it to the array:
$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval ,$end);
$array = array();
foreach($daterange as $date){
if ($date->format('N') < 6) {
$array[] = $date->format("Y-m-d");
}
}
Demo on 3v4l.org
Update
Based on comments from the OP, days to be included have $row[<dayname>] = 1. In that case, you can use this foreach loop, using l format to get the full day of week name and strtolower to convert to lowercase to use as an index into $row:
foreach($daterange as $date){
if ($row[strtolower($date->format('l'))]) {
$array[] = $date->format("D Y-m-d");
}
}
Demo on 3v4l.org

Printing dates between two other dates on PHP [duplicate]

This question already has answers here:
PHP: Return all dates between two dates in an array [duplicate]
(26 answers)
Closed 6 years ago.
I'm trying to print dates between two other dates. Here is my code:
$begin = date("d/m/y");
$end = date("d/m/y", strtotime("+1 month"));
$i = 0;
while( strtotime($begin) <= strtotime($end) ){
echo "$begin\n";
$i++;
$begin = date("d/m/y", strtotime("+$i day") );
}
You can execute the same code here:
http://sandbox.onlinephpfunctions.com/code/34c4b721553038f585806798121941bee0c66086
For some reason this code is printing just the dates between 25/01/2017 and 31/01/2017 instead of 25/01/2017 and 25/02/2017. I don't know what's wrong. Can someone help me?
strtotime() doesn't support dates in d/m/y format. It treats these dates as m/d/y.
To fix your code, use Y-m-d format in the first two lines.
On a sidenote, I'd recommend to use \DateTime classes to manipulate dates instead of strings and integers. Read more here: https://paulund.co.uk/datetime-php
<?php
error_reporting(-1);
ini_set('display_errors', true);
$begin = new DateTime();
$end = (new DateTime())->modify('+1 month');
$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($begin, $interval, $end);
foreach ($period as $date) {
echo $date->format('d/m/y')."<br/>";
}

get all the dates between start date and end date in php [duplicate]

This question already has answers here:
PHP: Return all dates between two dates in an array [duplicate]
(26 answers)
Closed 7 years ago.
I want to get all the dates between the start date and end date. I need to show a weekly data, and the data is split into day wise.
I have a start date as 2015-11-02 and end date as 2015-11-08. Then I need to print all the dates in between in a foreach loop.
How can I do that?
One could use string manipulation and basic math, but months with different number of days and leap years etc make this a futile approach.
It is better to use an instance of PHP DateTime and loop through that, which is a question already answered here:
$begin = new DateTime( '2010-05-01' );
$end = new DateTime( '2010-05-10' );
$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($begin, $interval, $end);
foreach ( $period as $dt )
echo $dt->format( "l Y-m-d H:i:s\n" );
The DateTime object has the added advantage of outputting various formats, useful when generating link names, URIs, SQL queries, etc.

get all weekdays in php [duplicate]

This question already has an answer here:
PHP: getting weekdays numbers of a given month
(1 answer)
Closed 10 years ago.
I am thingking if how to get all the date of weekdays between a given date
example: I have a date given 2013-01-01 and 2013-20-01
It must return all date of weekdays like 2013-01-01
how could this be done using php
thankz
Look into DatePeriod Class, this is available from PHP 5.3.
Here is an example from the site
$begin = new DateTime( '2012-08-01' );
$end = new DateTime( '2012-08-31' );
$end = $end->modify( '+1 day' );
$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval ,$end);
foreach($daterange as $date){
// You can manipulate the date here
echo $date->format("Ymd") . "<br>";
}
You can get the weekday from the timestamp using the multitude of formatting options avalaible:
echo date("D",strtotime("2013-01-01"));
tues

Find date difference in php?

Is there anyway to find the date difference in php? I have the input of from date 2003-10-17 and todate 2004-03-24. I need the results how many days is there within these two days. Say if 224 days, i need the output in days only.
I find the solution through mysql but i need in php. Anyone help me, Thanks in advance.
$start = new DateTime( '2003-10-17' );
$end = new DateTime( '2004-03-24' );
$diff = $start->diff( $end );
echo $diff->format( '%d days' );
...should do it.
For reference see DateTime and DateInterval.
Mind you though, this is only available as of PHP 5.3.
You can use the parse timestamp feature to convert dates to timestamps, subtract the timestamps, and then convert the resulting timestamp (seconds) to days:
floor((strtotime("2004-03-24") - strtotime("2003-10-17"))/86400);
Example is as below:
$startDate = new DateTime( '2013-04-01' ); //intialize start date
$endDate = new DateTime( '2013-04-30' ); //initialize end date
$holiday = array('2013-04-11','2013-04-25'); //this is assumed list of holiday
$interval = new DateInterval('P1D'); // set the interval as 1 day
$daterange = new DatePeriod($startDate, $interval ,$endDate);
foreach($daterange as $date){
if($date->format("N") <6 AND !in_array($date->format("Y-m-d"),$holiday))
$result[] = $date->format("Y-m-d");
}
echo "<pre>";print_r($result);
A detail blog is here: http://goo.gl/YOsfPX

Categories