two date ranges input all days into database - php

I have a database with a possible 10 dates in input into each entry. The User will choose a start and end date and I want all the dates to be inputted into the database for my calender page.
$date1 = '2013-01-31';
$date2 = '2013-02-05';
for($one = $date1;$one>$date2;$one = strtotime(date('m/d/Y',$one)." -1 day"))
{
echo $one;
}
firstly I need to extract all the dates. The above does not work. Meaning dates 31/01/02/03/04/05 need to be inputted into the database.

DateTime makes this so much easier:
For PHP 5.3+ Users
$start = new DateTime('2013-01-31');
$end = new DateTime('2013-02-05');
$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $dt)
{
echo $dt->format("m/d/Y") . PHP_EOL;
}
For PHP 5.2 Users
$start = new DateTime('2013-01-31');
$end = new DateTime('2013-02-05');
while ($start <= $end)
{
echo $start->format("m/d/Y") . PHP_EOL;
$start->modify("+1 day");
}
reference
DateTime
DatePeriod
DateInterval

Related

php get times between tow times like 1:30:00AM

this code get count hours between tow times
ineed out put like this
6:00:00AM
6:30:00AM
7:00:00AM
7:30:00AM
8:00:00AM
8:30:00AM
......
10:30:00PM
how i can do this
$start = date_create('6:00:00AM');
$end = date_create('11:00:00PM');
$diff = date_diff($end, $start);
print_r($diff->h);
Alternatively you could use DatePeriod to build the list of dates on the desired specification, as opposed to iterating over comparisons of the date and adding the increment.
$start = date_create('6:00:00AM');
$end = date_create('11:00:00PM');
$interval = \DateInterval::createFromDateString('30 minutes');
$periods = new \DatePeriod($start, $interval, $end);
foreach ($periods as $date) {
echo $date->format('h:i:sA') . \PHP_EOL;
}
Result: https://3v4l.org/FnEae
06:00:00AM
06:30:00AM
07:00:00AM
07:30:00AM
08:00:00AM
08:30:00AM
//..
10:30:00PM
Since \DateTime::diff() won't always show the total hours between dates with different days, you can count the hours between the two dates by using iterator_count on the DatePeriod of the desired interval:
$start = date_create('6:00:00AM');
$end = date_create('11:00:00PM');
$interval = \DateInterval::createFromDateString('1 hour');
$periods = new \DatePeriod($start, $interval, $end);
echo iterator_count($periods) . ' hours';
Result: https://3v4l.org/P75K7
17 hours
<?php
$start = date_create('6:00:00AM');
$end = date_create('11:00:00PM');
$halfHour = new DateInterval('PT1800S');
while ($start < $end) {
print ($start->format('h:i:sA').PHP_EOL);
$start->add($halfHour);
}
?>
Create 2 Datetime objects and a date interval object and iterate while less than the end Datetime add the interval each time.
$start = date_create('6:00:00AM');
$end = date_create('11:00:00PM');
$interval = new DateInterval('PT30M');
while ($start <= $end) {
echo $start->format('H:i:s');
$start->add($interval);
}

How to include the end date in a DatePeriod?

I am trying to get a Date range for all workdays this week. I have written the following code to do so.
Code
$begin = new DateTime('monday this week'); 2016-07-04
$end = clone $begin;
$end->modify('next friday'); // 2016-07-08
$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval, $end);
foreach($daterange as $date) {
echo $date->format('Y-m-d')."<br />";
}
Output
2016-07-04
2016-07-05
2016-07-06
2016-07-07
In the output friday is missing. I can fix this by doing $end->modify('next saturday') but I was wondering why the last day of a DatePeriod is not included in the range.
The iterator seems to check the time as well as the date, it excludes the end element if the time in the endDate is less that or equal to the time in the start date.
So ensure the time of the end date is at least a second greater that that of the start date.
// this will default to a time of 00:00:00
$begin = new DateTime('monday this week'); //2016-07-04
$end = clone $begin;
// this will default to a time of 00:00:00
$end->modify('next friday'); // 2016-07-08
$end->setTime(0,0,1); // new line
$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval, $end);
foreach($daterange as $date) {
echo $date->format('Y-m-d')."<br />";
}
Try this code
<?php
$begin = new DateTime('2016-07-04');
$end = clone $begin;
$end->modify('next friday'); // 2016-07-08
$end->modify('+1 day');
$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval, $end);
foreach ($daterange as $date) {
echo $date->format('Y-m-d') . PHP_EOL;
}
In PHP 8.2 you can use DatePeriod::INCLUDE_END_DATE as constructor option!
https://www.php.net/manual/en/class.dateperiod.php#dateperiod.constants.include-end-date

Laravel Create range of date with Carbon Interval

I want to get range of date for example:
$startDate = Carbon::now()->subWeek();
$endDate = Carbon::now();
$period = new DatePeriod($startDate, CarbonInterval::day(), $endDate);
foreach ($period as $row) {
$date = $row->format('Y-m-d');
echo $date. ', ';
}
Those code will return me this date:
2016-04-24, 2016-04-25, 2016-04-26, 2016-04-27, ... until 2016-05-23
But the problem is, the date not return today's date (2016-05-24).
How can I get today date using above code?
Thanks.
I can get today date buy changing $endDate = Carbon::now(); to $endDate = Carbon::now()->tomorrow();
But its a little bit odd when read the code.
$numberOfDays = $endDate->diffInDays($startDate);
foreach (range(0, $numberOfDays) as $day) {
$dates[] = $endDate->copy()->subDays($day)->format('Y-m-d');
}

How to check if month exist in specific period using PHP

How to check if month exist in specific period ($date1 and $date2)
$month = '2016-01';
$date1 = '2016-01-05';
$date2 = '2016-02-04;
First convert the month into a date, like the first day of the month. Then you can compare the dates to check if the month lies in between:
$month_day = date ('Y-m-01', strtotime($month) );
$date1_day = date ('Y-m-01', strtotime($date1) );
$date2_day = date ('Y-m-01', strtotime($date2) );
if ( ($month_day >= min($date1_day, $date2_day))
&& ($month_day <= max($date1_day, $date2_day)) )
{ }
I got a best answer for my question on his link The Answer
This answer get months between two dates
$start = (new DateTime('2016-01-05'))->modify('first day of this month');
$end = (new DateTime('2016-02-04'))->modify('first day of this month');
$interval = DateInterval::createFromDateString('1 month');
$period = new DatePeriod($start, $interval, $end);
$monthsArray = [];
foreach ($period as $dt) {
$monthsArray[] = $dt->format("Y-m"); // I put months exist in this period on array to check later if the $month exist on this array or not
}
You could search the strings using strpos(). http://php.net/manual/en/function.strpos.php
ex:
if (strpos($date1,$month) || strpos($date2,$month)){/* do stuff */}

Add datetime Value

I'm searching for the best way to echo day numbers in a week.
First i need to check what week there is.
And then echo all dates of the week into variables.
This is what i got:
//This Week Variable dates
$this_year = date('Y');
$this_week_no = date('W');
$this_week_mon = new DateTime();
$this_week_mon->setISODate($this_year,$this_week_no);
How do i rise tue by one day?
$this_week_tue = $this_week_mon ++;
You can use DateTime::modify():
$this_week_mon->modify('+1 day');
or DateTime::add() which accepts a DateInterval() object:
$this_week_mon->add(new DateInterval('P1D'));
You can loop through all of the days of the week using DatePeriod():
$start = new DateTime();
$start->setISODate($this_year,$this_week_no);
$end = clone $start;
$end->modify('+1 week');
$interval = new DateInterval('P1D');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $date) {
echo $date->format('N');
}
$this_week_mon->modify('+1 day');
Should increment $this_week_mon by one day. To increase by more, just use days;
$this_week_mon->modify('+27 day');
Would increment by 27 days.

Categories