Get all dates from last 30 days (datetime objects) - php

I am trying to get an array of datetime objects but failing miserably.
I want to take today's date, and get all dates within the last 30 days.
$today = new DateTime();
$begin = $today->sub(new DateInterval('P30D'));
$interval = new DateInterval('P1D'); // 1 Day
$dateRange = new DatePeriod($begin, $interval, $today);
$range = [];
foreach ($dateRange as $date) {
$range[] = $date->format('Y-m-d');
}
When I dump out $range, I get an empty array.
What am I doing wrong?

Change
$begin = $today->sub(new DateInterval('P30D'));
to
$begin = new DateTime();
$begin->sub(new DateInterval('P30D'));

the "sub" method modifies the source object, as well as outputting the object itself as the return value (really this is intended for method chaining). It doesn't just create a new object with the new date.
$begin = $today->sub(new DateInterval('P30D'));
modifies $today as well as outputting a copy which you then declare as $begin. This results in both objects having the same date, and thus there's no time interval over which to iterate.
See http://php.net/manual/en/datetime.sub.php
You need to create a separate object for your end date:
$begin = new DateTime();
$begin->sub(new DateInterval('P30D'));
$end = new DateTime();
$interval = new DateInterval('P1D'); // 1 Day
$dateRange = new DatePeriod($begin, $interval, $end);
$range = [];
foreach ($dateRange as $date) {
$range[] = $date->format('Y-m-d');
}
var_dump($range);
See it working at https://eval.in/867948

you overwrite $today so $today and $begin is exactly the same
$today = new DateTime();
$copy = clone $today;
$begin = $copy->sub(new DateInterval('P30D'));
$interval = new DateInterval('P1D'); // 1 Day
$dateRange = new DatePeriod($begin, $interval, $today);
$range = [];
foreach ($dateRange as $date) {
$range[] = $date->format('Y-m-d');
}
will work

Related

Array into Chart.js labels

I am trying to echo a variable that contains the last 30 days into tha label of a chart. I don't know what I'm missing.
> $today = new DateTime();
$begin = $today->sub(new DateInterval('P30D'));
$end = new DateTime();
$end = $end->modify('+1 day');
$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval, $end);
foreach ($daterange as $date) {
$d[] = $date->format("M j");
}
$labels = "'".implode("','",$d)."'";
The line from chart config
labels: [<? echo $labels; ?>],
This is the output: 'Feb 24','Feb 25','Feb 26','Feb 27','Feb 28','Mar 1','Mar 2','Mar 3','Mar 4','Mar 5','Mar 6','Mar 7','Mar 8','Mar 9','Mar 10','Mar 11','Mar 12','Mar 13','Mar 14','Mar 15','Mar 16','Mar 17','Mar 18','Mar 19','Mar 20','Mar 21','Mar 22','Mar 23','Mar 24','Mar 25','Mar 26'
I thought this was the correct version, but the chart doesn t work, it s blank.I don't know what I'm missing.
Thanks in advance

Checking how many weekends are within date range

How do I check in Laravel how many Saturdays or Sundays are in given date range between $from and $to?
You can do it with plain PHP like this:
$start = new DateTime('2016-07-07');
$end = new DateTime('2016-07-27');
$interval = new DateInterval('P1D');
$daterange = new DatePeriod($start, $interval ,$end);
$saturdays = 0;
foreach($daterange as $date){
$days = $date->format('D');
if ($days == 'Sat') {
$saturdays++;
}
}
echo $saturdays;

Weird PHP issue - DatePeriod does not show February while iterating through months.

This is my code
$from = '2014-10-01 00:00:00';
$to = '2015-05-31 23:30:00';
$start = new DateTime('#' . strtotime($from), new DateTimeZone('Asia/Dubai'));
$end = new DateTime('#' . strtotime($to), new DateTimeZone('Asia/Dubai'));
$interval = DateInterval::createFromDateString('1 month');
$period = new DatePeriod($start, $interval, $end);
$count = 0;
foreach ($period as $dt) {
echo $dt->format('Y-M').'<br>';
}
exit;
I need to get the month's starting date and month's ending date for a certain period. So, I use the DatePeriod.
But, this only shows this output.
2014-Sep
2014-Oct
2014-Nov
2014-Dec
2015-Jan
2015-Mar
2015-Apr
2015-May
And somehow the month February is missed.
Can anyone help me on this ?
Just remove strtotime and # from the code and it'll work fine. As there is no need to make a timestamp of your date value
$from = '2014-10-01 00:00:00';
$to = '2015-05-31 23:30:00';
$start = new DateTime($from, new DateTimeZone('Asia/Dubai'));
$end = new DateTime($to, new DateTimeZone('Asia/Dubai'));
$interval = DateInterval::createFromDateString('1 month');
$period = new DatePeriod($start, $interval, $end);
$count = 0;
foreach ($period as $dt) {
echo $dt->format('Y-M').'<br>';
}

How to make a for loop for some month

I want to make for loop in php pdo that will create me an json data but loop must be for an month.
I write this:
try {
for ($i=1; $i<=30; $i++) {
$temp = array();
$temp['ID'] = $i;
$output['data'][] = $temp;
}
$jsonTable = json_encode($output);
SO this return me 30 rows, for 30 days. Now I want to create a range etc. to make me a rows from 01.02.2014 to rest of month 28/29.02.2014 so February ...
How to make this possible? Some ideas?
If you can't read the manual for yourself:
$dateString = '01.02.2014';
$dt = new DateTime($dateString);
$daysInMonth = $dt->format('t');
will give you the number of days in the month specified in $dateString
One way to loop through the days in a month:
$dateString = '01.02.2014';
$startDate = new DateTime($dateString);
$period = new DateInterval('P1M');
$endDate = clone $startDate;
$endDate->add($period);
$dayPeriod = new DateInterval('P1D');
while ($startDate < $endDate) {
echo $startDate->format('Y-m-d'), PHP_EOL;
$startDate->add($dayPeriod);
}
Another way to get that list of dates for a month
$dateString = '01.02.2014';
$startDate = new DateTime($dateString);
$period = new DateInterval('P1M');
$endDate = clone $startDate;
$endDate->add($period);
$interval = new DateInterval('P1D');
$daterange = new DatePeriod($startDate, $interval ,$endDate);
foreach($daterange as $date){
echo $date->format("Y-m-d") . PHP_EOL;
}

Return all dates between multiple dates in a single array

$start = new DateTime('2013-08-16');
$interval = new DateInterval('P1D');
$end = new DateTime('2013-08-20');
$end->add(new DateInterval('P1D'));
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $date) {
echo $date->format('Y-m-d') . "<br />";
}
I found the above code to get all dates between two static dates.
What I would like is to get the dates between multiple $start $end pairs. The scenario is to associate the variables ($start, &end) with fields from a db in order to make multiple pairs and get the between dates of these pairs into an array.
Is it possible?
Thanks!
just write the code in a function like so
function find_dates_between( $start_date, $end_date) {
$start = new DateTime($start_date);
$interval = new DateInterval('P1D');
$end = new DateTime($end_date);
$end->add(new DateInterval('P1D'));
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $date) {
echo $date->format('Y-m-d') . "<br />";
}
}
After that just call the function
find_dates_between( $start_date, $end_date);
where $start_date and $end_date are extracted from you DB

Categories