Error during third saturday date fetching - php

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);
?>

Related

PHP print dates without sunday

I have this form with this input field
<input type="text" name="txt_datetimein" class="form-control datetime">
<input type="text" name="txt_datetimeout" class="form-control datetime">
<input type="text" name="txt_lenght" class="form-control">
I enter the first date and the second date and the length the length i precise the number of repetition
than i click next and i have all days without sunday
example if i put 5 in the length and datetimein 01-04-2017 8:00:00 and datetimeout is 01-04-2017 5:00:00
the output will be like that
Date IN Date Out Day
01-04-2017 8:00:00 01-04-2017 5:00:00 Saturday
03-04-2017 8:00:00 03-04-2017 5:00:00 Monday
04-04-2017 8:00:00 04-04-2017 5:00:00 Tuesday
05-04-2017 8:00:00 05-04-2017 5:00:00 Wednesday
06-04-2017 8:00:00 06-04-2017 5:00:00 Thursday
07-04-2017 8:00:00 07-04-2017 5:00:00 Friday
this my code but it's print all day
<?php
for($i=0;$i<=$lenght;$i++) {
$date = date('m/d/Y H:i:s', strtotime("+$i days", strtotime($datetimein)));
$edate = date('m/d/Y H:i:s', strtotime("+$i days", strtotime( $datetimeout)));
$is_sunday = date('l', strtotime($date)) == 'Sunday';
if ($is_sunday) {
$day = date('l', strtotime("+1+$i days",strtotime($datetimein)));
}
else {
$day = date('l', strtotime("+$i days",strtotime($datetimein)));
}
}
?>
How Can i solve my Problem ??!!
try this below code
$datetimein = "01-04-2017 8:00:00";
$datetimeout = "01-04-2017 5:00:00";
$lenght = 20;
for($i=0;$i<=$lenght;$i++) {
$date = date('m/d/Y H:i:s', strtotime("+$i days", strtotime($datetimein)));
$edate = date('m/d/Y H:i:s', strtotime("+$i days", strtotime( $datetimeout)));
$is_sunday = date('l', strtotime($date));
if($is_sunday == "Sunday")
{
$i=$i+1;
}
$day = date('l', strtotime("$i days",strtotime($datetimein)));
echo $day."<br>";
}
Use DateTime and all those objects. Simpler and cleaner :-)
<?php
$begin = new DateTime();
$end = clone $begin;
$end = $end->modify('+14 day');
$interval = new DateInterval('P1D');
$range = new DatePeriod($begin, $interval ,$end);
foreach($range as $date) {
if ($date->format('N') !== 7) {
echo $date->format('Y-m-d'), '<br>';
}
}
Date format N is the day of week as a number where 7 === Sunday.
Here is you programe
$datetimein = '01-04-2017 8:00:00';
$datetimeout= '01-04-2017 5:00:00';
$lenght = 5;
$i=0;
$days = array();
$dt = strtotime($datetimein);
while($i < $lenght){
if(date('D',$dt)!='Sun'){
$days[] = date('Y-m-d D',$dt);
$i++;
}
$dt = $dt+24*3600;
}
print_r($days);
In this line $days[] = date('Y-m-d D',$dt); change the format or save both in and out time whatever you want. $days will have you expected dates.
It looks like you have mis-spelled the variable length.
Check this.
<?php
$lenght = 5;
$in_temp = 0;
$out_temp = 0;
for($i=0;$i<=$lenght;$i++) {
$in = strtotime($datetimein) + $in_temp;
$out = strtotime($datetimeout) + $out_temp;
$date = date('m/d/Y H:i:s', strtotime("+$i days", $in));
$edate = date('m/d/Y H:i:s', strtotime("+$i days", $out));
$is_sunday = strtolower(date('l', strtotime($date))) == 'sunday';
if ($is_sunday) {
$in_temp += 86400 ; // Adding 1 day in seconds.
$day = date('l', (strtotime($date)+$in_temp));
}
else {
$day = date('l', (strtotime($date)));
}
echo $day."\n";
}
?>

looping date function php

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

PHP date - get beginning and ending day of given week from format W-m-Y

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";
}

Print months between -6 and +6 months

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

Why won't this for loop work for iterating through unix time?

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.

Categories