Issue with DatePeriod Class Output - php

I'm using DatePeriod to get date difference between two dates but it creates issue when I need to get data Year based format. Following is my work
$start_date = '2016-08-29 21:47:33';
$end_date = '2018-04-28 17:30:55';
function getDateRange($start_date, $end_date, $type, $format = 'Y-m-d')
{
$period = new DatePeriod(
new DateTime($start_date),
new DateInterval("P1{$type}"),
new DateTime($end_date)
);
$dates = [];
foreach($period as $dt)
{
$dates[] = $dt->format("$format");
}
return $dates;
}
print_r(getDateRange($start_date, $end_date, 'Y', 'Y'));
According to my function it should return
Array
(
[0] => 2016
[1] => 2017
[2] => 2018
)
But its been outputting only two of them
Array
(
[0] => 2016
[1] => 2017
)
If I'm using output format with Month & Year i.e. M Y with interval of Month then it works fine output for that
print_r(getDateRange($start_date, $end_date, 'M', 'M Y'));
Array
(
[0] => Aug 2016
[1] => Sep 2016
[2] => Oct 2016
[3] => Nov 2016
[4] => Dec 2016
[5] => Jan 2017
[6] => Mar 2017
[7] => Apr 2017
[8] => May 2017
[9] => Jun 2017
[10] => Jul 2017
[11] => Aug 2017
[12] => Sep 2017
[13] => Oct 2017
[14] => Nov 2017
[15] => Dec 2017
[16] => Jan 2018
[17] => Feb 2018
[18] => Mar 2018
[19] => Apr 2018
)

I have modified changes as per documentation link,
Here is the code,
function getDateRange($start_date, $end_date, $type, $format = 'Y-m-d')
{
$start_date = date_create_from_format("Y-m-d H:i:s", $start_date);
$end_date = date_create_from_format("Y-m-d H:i:s", $end_date);
$endDateInt = new DateInterval( "P1Y" );
$end_date->add( $endDateInt );
$period = new DatePeriod(
$start_date,
new DateInterval("P1{$type}"),
$end_date
);
$dates = [];
foreach($period as $dt)
{
$dates[] = $dt->format("$format");
}
return $dates;
}
$start_date = '2016-08-29 21:47:33';
$end_date = '2018-04-28 17:30:55';
print_r(getDateRange($start_date, $end_date, 'Y', 'Y'));
Here is working link.

You can see the reason why in your second output. The last date goes only until April, and the first one is in August. So it's indeed not two full years, and your first result is correct. If you want to make it be the number of "years involved", without regard for actual difference, you could simulate this by setting the second date to the same day and month as the first one, if $type is Y:
<?php
$start_date = '2016-08-29 23:47:33';
$end_date = '2018-09-28 17:30:55';
function getDateRange($start_date, $end_date, $type, $format = 'Y-m-d')
{
$start = new DateTime($start_date);
$end = new DateTime($end_date);
if ($type === "Y") {
$start->setTime(0, 0, 0);
$end->setDate($end->format("Y"), $start->format("m"), $start->format("d"));
$end->setTime(0, 0, 1);
}
$period = new DatePeriod(
$start,
new DateInterval("P1{$type}"),
$end
);
$dates = [];
foreach($period as $dt)
{
$dates[] = $dt->format("$format");
}
return $dates;
}
print_r(getDateRange($start_date, $end_date, 'Y', 'Y'));
Demo

Related

How to get datetime range between two datetimestamps with an interval in php

I am trying to write a program to dynamically add interval and find datetime between two dates in php.
I am getting Startdatetime, Enddatetime, Interval from the user.
If the
Start date is 2020-02-17 00:00:00, end date is 2020-02-17 08:00:00, and interval to be added to is 2hrs,
then I am trying to print all datetime ranges like
Array(
[0] => 2020-02-17 00:00:00
[1] => 2020-02-17 02:00:00
[2] => 2020-02-17 04:00:00
[3] => 2020-02-17 06:00:00
[4] => 2020-02-17 08:00:00
)
I tried with dateperiod, but doesn't work as it gives only start & end date
$period = new DatePeriod(
new DateTime($from_datetime),
new DateInterval('PT$hoursH'),
new DateTime($to_datetime)
);
Please help me to get all datetime ranges.
Using this:
<?php
$begin = new DateTime('2020-02-17 00:00:00');
$end = new DateTime('2020-02-17 08:00:01');
$interval = DateInterval::createFromDateString('2 hours');
$period = new DatePeriod($begin, $interval, $end);
$myDates = [];
foreach ($period as $dt) {
$myDates[] = $dt->format("Y-m-d H:i:s");
}
Now executing:
print_r($myDates);
gives you
Array (
[0] => 2020-02-17 00:00:00
[1] => 2020-02-17 02:00:00
[2] => 2020-02-17 04:00:00
[3] => 2020-02-17 06:00:00
[4] => 2020-02-17 08:00:00
)
You can use the date parser of PHP, its pretty intelligent.
$startDate = new DateTime('2020-02-17 00:00:00');
$endDate = new DateTime('2020-02-17 08:00:00');
$intervalInHrs = 2;
while ($startDate <= $endDate) {
$output[] = $startDate->format("Y-m-d H:i:s");
$startDate->modify("+$intervalInHrs Hours");
}
Output:
Array
(
[0] => 2020-02-17 00:00:00
[1] => 2020-02-17 02:00:00
[2] => 2020-02-17 04:00:00
[3] => 2020-02-17 06:00:00
[4] => 2020-02-17 08:00:00
)
$from_datetime = new DateTime('2020-02-17 00:00:00');
$to_datetime = new DateTime('2020-02-17 08:00:00');
$hours = 2;
$interval = new DateInterval('PT'.$hours.'H');
$period = new DatePeriod($from_datetime, $interval, $to_datetime);
$dateArray = [];
foreach ($period as $dt) {
$dateArray[] = $dt->format("Y-m-d H:i:s");
}
print_r($dateArray);exit;
Above code gave me the expected output as
Array
(
[0] => 2021-02-17 00:00:00
[1] => 2021-02-17 02:00:00
[2] => 2021-02-17 04:00:00
[3] => 2021-02-17 06:00:00
[4] => 2021-02-17 08:00:00
)

PHP: print all days between a given day and end of month

I don't want to return DATE (Y-m-d).
I need to print all days until end of month from a given day independently of the month or year.
I tried both [$array as $i] - [$array as $key] and didn't work.
$myday (for example = 19)
return $days
would result:
Array
[0] => 20
[1] => 21
[2] => 22
[3] => 23
...
[31] => 31 || [30] => 30 || [28] => 28
I would need each value for $days to compare each to another field.
Didn't try to use $myday as regular number instead of treating as date. And not use strtotime, mktime....
EDITING
Need something very simple like this one:
$output = array();
for ($i=$myday+1;$i<=31 || $i<=30 || $i<=28;$i++) {
$output[] = $i;
}
But print_r won't do it, I need to return as each value to use in different if conditions
This is easily done using DateTime(), DateInterval(), DatePeriod(), and relative date formats.
$start = (new DateTime())->setDate(date('Y'), date('m'), $myday + 1);
$end = new DateTime('first day of next month');
$interval = new DateInterval('P1D');
$period = new DatePeriod($start, $interval, $end);
$days = array();
foreach($period as $date) {
$days[] = $date->format('d');
}
Results
Array
(
[0] => 20
[1] => 21
[2] => 22
[3] => 23
[4] => 24
[5] => 25
[6] => 26
[7] => 27
[8] => 28
[9] => 29
[10] => 30
[11] => 31
)
Demo

Find week days date from range of the two dates

$start_date = "2013-05-01";
$last_date = "2013-08-30";
How can I get dates of tuesdays and thursdays between these two dates?
<?php
$start = new DateTime('2013-05-01');
$end = new DateTime('2013-08-30');
$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $dt) {
if ($dt->format("N") == 2 || $dt->format("N") == 4) {
echo $dt->format("l Y-m-d") . "<br>\n";
}
}
See it in action
What this code does:
Creates a starting date object using DateTime.
Creates a starting date object using DateTime.
Creates a DateInterval object to represent our interval of time to iterate through. In this case 1 day.
Creates a DatePeriod object to manage these objects.
Using DatePeriod, it iterates through the date starting with the starting date and ending at the end date. We use DateTime::format() with the N parameter to get the day number of the week. If the day number of the week is 2 (Tuesday) or 4 (Thursday) echo out it's value.
Some PHP-Fu
$start_date = '2013-05-01';
$last_date = '2013-08-30';
$dates = range(strtotime($start_date), strtotime($last_date),86400);
$days = array('tuesday' => array(), 'thursday' => array());
array_map(function($v)use(&$days){
if(date('D', $v) == 'Tue'){
$days['tuesday'][] = date('Y-m-d', $v);
}elseif(date('D', $v) == 'Thu'){
$days['thursday'][] = date('Y-m-d', $v);
}
}, $dates); // Requires PHP 5.3+
print_r($days);
Output
Array
(
[tuesday] => Array
(
[0] => 2013-05-07
[1] => 2013-05-14
[2] => 2013-05-21
[3] => 2013-05-28
[4] => 2013-06-04
[5] => 2013-06-11
[6] => 2013-06-18
[7] => 2013-06-25
[8] => 2013-07-02
[9] => 2013-07-09
[10] => 2013-07-16
[11] => 2013-07-23
[12] => 2013-07-30
[13] => 2013-08-06
[14] => 2013-08-13
[15] => 2013-08-20
[16] => 2013-08-27
)
[thursday] => Array
(
[0] => 2013-05-02
[1] => 2013-05-09
[2] => 2013-05-16
[3] => 2013-05-23
[4] => 2013-05-30
[5] => 2013-06-06
[6] => 2013-06-13
[7] => 2013-06-20
[8] => 2013-06-27
[9] => 2013-07-04
[10] => 2013-07-11
[11] => 2013-07-18
[12] => 2013-07-25
[13] => 2013-08-01
[14] => 2013-08-08
[15] => 2013-08-15
[16] => 2013-08-22
[17] => 2013-08-29
)
)
Online demo
$start_date = strtotime("2013-05-01");
$last_date = strtotime("2013-08-30");
while ($start_date <= $last_date) {
$start_date = strtotime('+1 day', $start_date);
if (date('N',$start_date) == 2 || date('N',$start_date) == 4){
echo date('Y-m-d', $start_date).PHP_EOL;
}
}
<?php echo date('Y-m-d', strtotime('next thursday', strtotime($start_date)));
Also for tuesday ofcourse
Please use the following function for your solution,
function daycount($day, $startdate, $lastdate, $counter=0)
{
if($startdate >= $lastdate)
{
return $counter;
}
else
{
return daycount($day, strtotime("next ".$day, $startdate), ++$counter);
}
}
$start_date = "2013-05-01";
$last_date = "2013-08-30";
echo "Tuesday Count - ".daycount("tuesday", strtotime($start_date), strtotime($last_date));
echo "<br/>";
echo "Thursday Count - ".daycount("thursday", strtotime($start_date), strtotime($last_date));
Try with this
$startDate = strtotime($start_date);
$endDate = strtotime($last_date);
while ($startDate < $endDate) {
echo date('Y-m-d', $startDate ). "\n";
// Give the condition to find last Tuesday
$startDate = strtotime( 'next Tuesday', $startDate );
}
With DateTime:
$start_date = "2013-05-01";
$last_date = "2013-08-30";
$start = new DateTime($start_date);
$clone = clone $start;
$start->modify('next thursday');
$thursday=$start->format('Y-m-d');
$clone->modify('next tuesday');
$tuesday=$clone->format('Y-m-d');
echo $thursday; //2013-05-02
echo $tuesday; //2013-05-07
We need to objects because if in interval tuesday is before thursday we will have next tuesday. But you can modify little code to use one object.
With the help of few php date functions this can be solved easily..
<?php
// Create the from and to date
$start_date = strtotime("2013-05-01");
$last_date = strtotime("2013-08-30");
// Get the time interval to get the tue and Thurs days
$no_of_days = ($last_date - $start_date) / 86400; //the diff will be in timestamp hence dividing by timestamp for one day = 86400
$get_tue_thu_days = array();
// Loop upto the $no_of_days
for($i = 0; $i < $no_of_days; $i++) {
$temp = date("D", $start_date);
if($temp == "Tue" || $temp == "Thu") {
$get_tue_thu_days[] = date("D/M/Y", $start_date); //formating date in Thu/May/2013 formate.
}
$start_date += 86400;
}
print_r($get_tue_thu_days);
if you have a reference date which you know is a tuesday/thursday you can find days which are a multiple of 7 days from your reference date, these days will always be the same day of the week.

Year value error from PHP date() [duplicate]

This question already has answers here:
Display all the week numbers between two dates in PHP [duplicate]
(3 answers)
Closed 8 years ago.
Given a start and end date, I need to generate an array with year and week values.
For example:
Start Date: 2013-01-01
End Date: 2013-02-23
Generated Array:
Array ( [0] => Array ( [week] => 01 [year] => 2013 )
[1] => Array ( [week] => 02 [year] => 2013 )
[2] => Array ( [week] => 03 [year] => 2013 )
[3] => Array ( [week] => 04 [year] => 2013 )
[4] => Array ( [week] => 05 [year] => 2013 )
[5] => Array ( [week] => 06 [year] => 2013 )
[6] => Array ( [week] => 07 [year] => 2013 )
[7] => Array ( [week] => 08 [year] => 2013 ) )
Here is the code I'm using to generate this:
public static function getYearWeekRange($startdate, $enddate) {
$array = array();
$starttime = strtotime($startdate);
$endtime = strtotime($enddate);
while ($starttime <= $endtime) {
$year = date('Y', $starttime);
$week = date('W', $starttime);
$array[] = array('week' => $week, 'year' => $year);
$starttime = strtotime('+1 week', $starttime);
}
return $array;
}
My problem is that when I generate certain date ranges, I don't get the correct year value at the start of the 2013 year. For example:
Start Date: 2012-01-01
End Date: 2013-02-23
In this case, where it should have an subarray with year = 2013 and week = 01, it actually has it's year value equal to 2012.
If I were to switch the start date to 2013-01-05 for example, then there is no problem.
Can anyone offer a solution that would guarantee that my year and week values are always correct?
I was able to fix my problem using the following:
public static function getWeekRange($startdate, $enddate) {
$array = array();
$p = new DatePeriod(
new DateTime($startdate),
new DateInterval('P1W'),
new DateTime($enddate)
);
foreach ($p as $w) {
$array[] = array('year' => $w->format('o'), 'week' => $w->format('W'));
}
return $array;
}

Wordpress IXR_Date wrong date

This is my php code:
echo date('m/d/Y h:i:s A T'),"<br>";
$match_date = strtotime(date('m/d/Y h:i:s A T'));
echo "match_date= ", $match_date, "<br>";
$test = new IXR_Date($match_date);
print_r($test);
But return wrong month and day value:
08/19/2012 07:38:10 AM PDT
match_date= 1345387090
IXR_Date Object ( [year] => 2012 [month] => 2012 [day] => 2012 [hour] => 07 [minute] => 38 [second] => 10 )
Whats wrong? How can I use IXR_Date properly?
Use time() function instread of strtotime over date:
$test = new IXR_Date(time());

Categories