I have table name "guest_room" in database, the fields of guest_room are "arrival" and "departure", the value of arrival is "2016-12-27" and the value of departure is "2016-12-31".
in my php file I want to show data from arrival to departure date, here my code :
$date = $g[arrival];
$end_date = $g[departure];
while (strtotime($date) <= strtotime($end_date)) {
$date = date ("Y-m-d", strtotime("+1 day", strtotime($date)));
echo "$date<br>";
}
but in my result show the date from "2016-12-28" until "2017-01-01"
what I want is, I want to show the date from "2016-12-27" until "2016-12-31"
I know I shouldn't use mysql_ but this is no point, help me please
Just put your incrementation at the end of your loops body:
$date = $g[arrival];
$end_date = $g[departure];
while (strtotime($date) <= strtotime($end_date)) {
// Switched these lines so the increment is at the end of the body
echo "$date<br>";
$date = date ("Y-m-d", strtotime("+1 day", strtotime($date)));
}
Try this :
$date = $g[arrival];
$end_date = $g[departure];
while (strtotime($date) < strtotime($end_date)) {
$date = date ("Y-m-d", strtotime("+1 day", strtotime($date)));
echo "$date<br>";
}
/**
* Generate an array of string dates between 2 dates
*
* #param string $start Start date
* #param string $end End date
* #param string $format Output format (Default: Y-m-d)
*
* #return array
*/
function getDatesFromRange($start, $end, $format = 'Y-m-d') {
$array = array();
$interval = new DateInterval('P1D');
$realEnd = new DateTime($end);
$realEnd->add($interval);
$period = new DatePeriod(new DateTime($start), $interval, $realEnd);
foreach($period as $date) {
$array[] = $date->format($format);
}
return $array;
}
Then, you would call the function as expected:
getDatesFromRange('2010-10-01', '2010-10-05');
Simply echo date first and then increment the date
while (strtotime($date) <= strtotime($end_date)){
echo "$date<br>";
$date = date ("Y-m-d", strtotime("+1 day", strtotime($date)));
}
Try this code,
$date = "2016-12-27" ;
$end_date = "2016-12-31";
while (strtotime($date) <= strtotime($end_date)) {
echo "$date<br>";
$date = date ("Y-m-d", strtotime("+1 day", strtotime($date)));
}
$date = "2016-12-27";
$end_date = "2016-12-31";
while (strtotime("+1 day", strtotime($date)) <= strtotime($end_date)) {
echo "$date<br>";
$date = date ("Y-m-d", strtotime("+1 day", strtotime($date)));
}
echo "$date<br>";
check screenshot
Related
I have an array of dates in format W-m-Y.
From e.g. 34-08-2016 I would like to get something like 20-08-2016 - 26-08-2016. Those days from requested format, aren't real.
Any idea how to tackle it?
try this
function getStartAndEndDate($week, $year) {
$dto = new DateTime();
$dto->setISODate($year, $week);
$ret['week_start'] = $dto->format('Y-m-d');
$dto->modify('+6 days');
$ret['week_end'] = $dto->format('Y-m-d');
return $ret;
}
$week_array = getStartAndEndDate(34,2016);
echo "start date ".date('d-m-Y',strtotime($week_array['week_start'])).'<br>';
echo "End date ".date('d-m-Y',strtotime($week_array['week_end']));
Try this, hope this helps.., also you can optimize it..
$date = "34-08-2016";
list($week_no, $month, $year) = explode("-", $date);
$date_obj = new DateTime();
$date_obj->setISODate($year,$week_no);
$day = $date_obj->format('w');
$week_start = date('m-d-Y', strtotime('-'.$day.' days', strtotime($date_obj->format('Y-m-d'))));
$week_end = date('m-d-Y', strtotime('+'.(6-$day).' days', strtotime($date_obj->format('Y-m-d'))));
Try this,
function getWeekDates($year, $week)
{
$from = date("Y-m-d", strtotime("{$year}-W{$week}-1")); //Returns the date of monday in week
$to = date("Y-m-d", strtotime("{$year}-W{$week}-7")); //Returns the date of sunday in week
return $from ." - ". $to;
//return "Week {$week} in {$year} is from {$from} to {$to}.";
}
$year = 2016;
$week = '34';
echo getWeekDates($year, $week);
DEMO
Try this
<?php
$date = '38-10-2016';
$date = explode('-',$date);
echo date("Y-m-d", strtotime( "$date[2]W$date[0]".'monday this week' ) ), "\n";
echo date("Y-m-d", strtotime( "$date[2]W$date[0]".'sunday this week' ) ), "\n";
This will collect the week number of the date you are given, and look for the Monday and Sunday of that week.
You can try this
function date_of_week($date)
{
echo date("d-m-Y",$date)."\n";
$day_of_week = date('N', $date); # 0->sunday, 1-> monday etc...
echo "\tday of week:$day_of_week\n";
#I assume you begin the week on monday.
$start_week_date = $date - ($day_of_week-1)*3600*24;
$end_week_date = $start_week_date + 6*3600*24;
return date('d-m-Y', $start_week_date)." - ".date('d-m-Y', $end_week_date);
}
for($i=16;$i<24;$i++)
{
echo date_of_week(mktime(0,0,0,10,$i,2016))."\n\n";
}
I'm trying to echo months from 1 year range, like example date 02-2016
I want months between (02-2016 - 6months) and (02-2016 + 6 months)
$now = strtotime(date('d-m-Y'));
$start = strtotime('-6 months');
$end = strtotime('+6 months');
while($start < $end) {
$links .= "".date('F', $start)."";
$start = strtotime($start+'1 month');
}
when echoing $links, I just get "August" echoed.
Define your start date and end date as below:-
$start = $month =strtotime("-6 months", strtotime('20015-02-01'));
$end = strtotime("+6 months", strtotime('20015-02-01'));
while($month < $end)
{
echo date('F Y', $month), PHP_EOL;
$month = strtotime("+1 month", $month);
}
Hope it will help you :)
Try:
$now = strtotime(date('d-m-Y'));
$start = strtotime('-6 months');
$end = strtotime('+6 months');
$links = "";
while($start < $end) {
$links .= "".date('F', $start)."";
$start = strtotime('+1 month', $start);
}
for strtotime the reference point is the second parameter: http://php.net/strtotime
I want to fetch third Saturday and I am using php function for that, that i know.
But I am getting wrong data while fetching from an error.
Here is my code:
$frmdate = 2015-06-05;
$todate = 2015-08-31;
for ($date = strtotime($frmdate); $date <= strtotime($todate); $date = strtotime("+1 day", $date))
{
$custom_day = date("Y-m-d", $date);
$custom_third_sat[] = date('Y-m-d', strtotime('third Saturday "'.$custom_day.'"'));
}
echo "<pre>";
print_r($custom_third_sat);
Where am I wrong?
Every Months contain only one "third saturday" , so no need to do more looping of days. Just try this Code Once.
$frmdate = "2015-06-05";
$todate = "2015-08-31";
$custom_third_sat=array();
for ($date = date("Y-m-01", strtotime($frmdate)); $date <= $todate; $date = date("Y-m-01",strtotime($date."+1 Month"))) {
if($date>$todate){
break;
}
$t_date=date('Y-m-d', strtotime($date.' third Saturday'));
if($t_date>=$frmdate && $t_date<=$todate)
{
$custom_third_sat[] = $t_date;
}
}
echo "<pre>";print_r($custom_third_sat);
you should use of like third saturday of:try this
$custom_third_sat[] = date('Y-m-d', strtotime("third saturday of $custom_day"));
your full code can be something like this:
$frmdate = '2015-06-05';
$todate = '2015-08-31';
for ($date = strtotime($frmdate); $date <= strtotime($todate); $date = strtotime("+1 day", $date))
{
$custom_day = date("Y-m-d", $date);
if(!isset($custom_third_sat[date('Y-m-d', strtotime("third saturday of $custom_day"))])){
$custom_third_sat[date('Y-m-d', strtotime("third saturday of $custom_day"))] = date('Y-m-d', strtotime("third saturday of $custom_day"));
}
}
echo "<pre>";
print_r($custom_third_sat);
You are just missing the quotes to dates
<?php
$frmdate = '2015-06-05';
$todate = '2015-08-31';
for ($date = strtotime($frmdate); $date <= strtotime($todate); $date = strtotime("+1 day", $date))
{
echo"assa";
$custom_day = date("Y-m-d", $date);
$custom_third_sat[] = date('Y-m-d', strtotime('third Saturday "'.$custom_day.'"'));
}
echo "<pre>";
print_r($custom_third_sat);
?>
I am trying to populate a select list with time.
I want to create the select list so that it starts from the starting date and then ends six months later.
I've created this for loop for now but it doesn't work:
$dateSelectList = '';
$startDate = $c->getStartDate(92);
$endDate = intval( strtotime('+6 month', $startdate) );
$i = 1;
$tempDate = 0;
for($date = $startdate; $date <= $endDate ; $date = strtotime('+1 day', $date))
{
$dateSelectList .= '<option id="select'.$i.'" value="'.$date.'">'.$date.'</option>';
$i++;
}
$dateSelectList .= '</select>';
I think it's the last field in the for loop but I don't know how to get around it.
I've changed it to $date = strtotime('+1 day', $date) and it works now.
Thanks a lot !
In each iteration, you're resetting the date to the start date plus one day. I.e., you're just using the same date over and over each iteration:
for($date = $startdate; $date <= $endDate ; $date = strtotime('+1 day', $startdate))
Change your for loop so that it keeps adding on to $date instead:
for($date = $startdate; $date <= $endDate ; $date = strtotime('+1 day', $date))
There are plenty of solutions. One of them may be:
Code
$startdate = time(); // today;
$enddate = strtotime('+6 months', $startdate);
while ($startdate <= $enddate) {
echo date('Y-m-d', $startdate) . "<br/>";
$startdate = strtotime('+1 day', $startdate);
}
Output
2012-03-26
2012-03-27
2012-03-28
2012-03-29
2012-03-30
2012-03-31
2012-04-01
...
2012-09-24
2012-09-25
2012-09-26
Now, modify code and create your selector as you like.
Change first line to
$year = 2012;
$month = 3;
$day = 26;
$startdate = strtotime("$year-$month-$day 00:00:00 UTC");
and create your custom $startdate.
Complete selector code
$year = 2012;
$month = 2;
$day = 3;
$startdate = strtotime("$year-$month-$day 00:00:00 UTC");
$enddate = strtotime('+6 months', $startdate);
$doc = "<select>"; $i=1;
while ($startdate <= $enddate) {
$dt = date('Y-m-d', $startdate);
$doc .= "<option id=\"select$i\" value=\"$dt\">$dt</option>";
$startdate = strtotime('+1 day', $startdate);
$i++;
}
$doc .= "</select>";
echo $doc;
Output
More elegant solution is to put it all into function like this
function createSelector($day, $month, $year) {
$startdate = strtotime("$year-$month-$day 00:00:00 UTC");
$enddate = strtotime('+6 months', $startdate);
$doc = "<select>"; $i=1;
while ($startdate <= $enddate) {
$dt = date('Y-m-d', $startdate);
$doc .= "<option id=\"select$i\" value=\"$dt\">$dt</option>";
$startdate = strtotime('+1 day', $startdate);
$i++;
}
$doc .= "</select>";
return $doc;
}
and call it this way
$selectorCode = createSelector(26, 3, 2012);
echo $selectorCode;
Cheers!
The problem is, indeed, with this bit of code: $date = strtotime('+1 day', $startdate) ...
$startdate is never being changed, therefore, $date is never being changed. You'll want something more like $date = strtotime('+1 day', $date) in order for the loop to work properly.
I'm starting with a date 2010-05-01 and ending with 2010-05-10. How can I iterate through all of those dates in PHP?
$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");
}
This will output all days in the defined period between $start and $end. If you want to include the 10th, set $end to 11th. You can adjust format to your liking. See the PHP Manual for DatePeriod. It requires PHP 5.3.
This also includes the last date
$begin = new DateTime( "2015-07-03" );
$end = new DateTime( "2015-07-09" );
for($i = $begin; $i <= $end; $i->modify('+1 day')){
echo $i->format("Y-m-d");
}
If you dont need the last date just remove = from the condition.
Converting to unix timestamps makes doing date math easier in php:
$startTime = strtotime( '2010-05-01 12:00' );
$endTime = strtotime( '2010-05-10 12:00' );
// Loop between timestamps, 24 hours at a time
for ( $i = $startTime; $i <= $endTime; $i = $i + 86400 ) {
$thisDate = date( 'Y-m-d', $i ); // 2010-05-01, 2010-05-02, etc
}
When using PHP with a timezone having DST, make sure to add a time that is not 23:00, 00:00 or 1:00 to protect against days skipping or repeating.
Copy from php.net sample for inclusive range:
$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){
echo $date->format("Ymd") . "<br>";
}
Here is another simple implementation -
/**
* Date range
*
* #param $first
* #param $last
* #param string $step
* #param string $format
* #return array
*/
function dateRange( $first, $last, $step = '+1 day', $format = 'Y-m-d' ) {
$dates = [];
$current = strtotime( $first );
$last = strtotime( $last );
while( $current <= $last ) {
$dates[] = date( $format, $current );
$current = strtotime( $step, $current );
}
return $dates;
}
Example:
print_r( dateRange( '2010-07-26', '2010-08-05') );
Array (
[0] => 2010-07-26
[1] => 2010-07-27
[2] => 2010-07-28
[3] => 2010-07-29
[4] => 2010-07-30
[5] => 2010-07-31
[6] => 2010-08-01
[7] => 2010-08-02
[8] => 2010-08-03
[9] => 2010-08-04
[10] => 2010-08-05
)
$startTime = strtotime('2010-05-01');
$endTime = strtotime('2010-05-10');
// Loop between timestamps, 1 day at a time
$i = 1;
do {
$newTime = strtotime('+'.$i++.' days',$startTime);
echo $newTime;
} while ($newTime < $endTime);
or
$startTime = strtotime('2010-05-01');
$endTime = strtotime('2010-05-10');
// Loop between timestamps, 1 day at a time
do {
$startTime = strtotime('+1 day',$startTime);
echo $startTime;
} while ($startTime < $endTime);
User this function:-
function dateRange($first, $last, $step = '+1 day', $format = 'Y-m-d' ) {
$dates = array();
$current = strtotime($first);
$last = strtotime($last);
while( $current <= $last ) {
$dates[] = date($format, $current);
$current = strtotime($step, $current);
}
return $dates;
}
Usage / function call:-
Increase by one day:-
dateRange($start, $end); //increment is set to 1 day.
Increase by Month:-
dateRange($start, $end, "+1 month");//increase by one month
use third parameter if you like to set date format:-
dateRange($start, $end, "+1 month", "Y-m-d H:i:s");//increase by one month and format is mysql datetime
For Carbon users
use Carbon\Carbon;
$startDay = Carbon::parse("2021-08-01");
$endDay= Carbon::parse("2021-08-05");
$period = $startDay->range($endDay, 1, 'day');
When I print the data
[
Carbon\Carbon #1627790400 {#4970
date: 2021-08-01 00:00:00.0 America/Toronto (-04:00),
},
Carbon\Carbon #1627876800 {#4974
date: 2021-08-02 00:00:00.0 America/Toronto (-04:00),
},
Carbon\Carbon #1627963200 {#4978
date: 2021-08-03 00:00:00.0 America/Toronto (-04:00),
},
Carbon\Carbon #1628049600 {#5007
date: 2021-08-04 00:00:00.0 America/Toronto (-04:00),
},
Carbon\Carbon #1628136000 {#5009
date: 2021-08-05 00:00:00.0 America/Toronto (-04:00),
},
]
This is Laravel data dump using dd($period->toArray());. You can now iterate through $period if you want with a foreach statement.
One important note - it includes both the edge dates provided to method.
For more cool date related stuff, do check out the Carbon docs.
here's a way:
$date = new Carbon();
$dtStart = $date->startOfMonth();
$dtEnd = $dtStart->copy()->endOfMonth();
$weekendsInMoth = [];
while ($dtStart->diffInDays($dtEnd)) {
if($dtStart->isWeekend()) {
$weekendsInMoth[] = $dtStart->copy();
}
$dtStart->addDay();
}
The result of $weekendsInMoth is array of weekend days!
If you're using php version less than 8.2 and don't have the DatePeriod::INCLUDE_END_DATE const. I wrote a method that returns an array of \DateTimeImmutable.
This works with a start date before, the same or after the end date.
/**
* #param DateTimeImmutable $start
* #param DateTimeImmutable $end
* #return array<\DateTimeImmutable>
*/
public static function getRangeDays(\DateTimeImmutable $start, \DateTimeImmutable $end): array
{
$startDate = $start;
$endDate = $end;
$forwards = $endDate >= $startDate;
$carryDate = $startDate;
$days = [];
while (true) {
if (($forwards && $carryDate > $end) || (!$forwards && $carryDate < $end)) {
break;
}
$days[] = $carryDate;
if ($forwards) {
$carryDate = $carryDate->modify('+1 day');
} else {
$carryDate = $carryDate->modify('- 1 day');
}
}
return $days;
}
$date = new DateTime($_POST['date']);
$endDate = date_add(new DateTime($_POST['date']),date_interval_create_from_date_string("7 days"));
while ($date <= $endDate) {
print date_format($date,'d-m-Y')." AND END DATE IS : ".date_format($endDate,'d-m-Y')."\n";
date_add($date,date_interval_create_from_date_string("1 days"));
}
You can iterate like this also, The $_POST['date'] can be dent from your app or website
Instead of $_POST['date'] you can also place your string here "21-12-2019". Both will work.
<?php
$start_date = '2015-01-01';
$end_date = '2015-06-30';
while (strtotime($start_date) <= strtotime($end_date)) {
echo "$start_daten";
$start_date = date ("Y-m-d", strtotime("+1 days", strtotime($start_date)));
}
?>
If you use Laravel and want to use Carbon the correct solution would be the following:
$start_date = Carbon::createFromFormat('Y-m-d', '2020-01-01');
$end_date = Carbon::createFromFormat('Y-m-d', '2020-01-31');
$period = new CarbonPeriod($start_date, '1 day', $end_date);
foreach ($period as $dt) {
echo $dt->format("l Y-m-d H:i:s\n");
}
Remember to add:
use Carbon\Carbon;
use Carbon\CarbonPeriod;