PHP print dates without sunday - php

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

Related

Get whole week date starting from sunday to saturday from given date

Hello guys i am working in php and my requirement is to get complete week dates from given date as i need to calculate weekly working hour. And week must be started from sunday to saturday not monday to sunday. I have code which works properly for other days of week except sunday. it means if give any dates from monday to saturday it works properly but if i give sunday's date it give last week's dates. please check my code and advise me for better solution.
$days = array();
$ddate = "2018-01-07";
$date = new DateTime($ddate);
$week = $date->format("W");
$y = date("Y", strtotime($ddate));
echo "Weeknummer: $week"."<br>";
echo "Year: $y"."<br>";
for($day=0; $day<=6; $day++)
{
$days[$day] = date('Y-m-d', strtotime($y."W".$week.$day))."<br>";
}
print_r($days);
Using the DateTime, DateInterval and DatePeriod classes you could do it like this perhaps
function getperiod( $start ){
return new DatePeriod(
new DateTime( $start ),
new DateInterval('P1D'),
new DateTime( date( DATE_COOKIE, strtotime( $start . '+ 7days' ) ) )
);
}
$start='2018-01-07';
$period=getperiod( $start );
foreach( $period as $date ){
echo $date->format('l -> Y-m-d') . '<br />';
}
Which returns
Sunday -> 2018-01-07
Monday -> 2018-01-08
Tuesday -> 2018-01-09
Wednesday -> 2018-01-10
Thursday -> 2018-01-11
Friday -> 2018-01-12
Saturday -> 2018-01-13
Or, by modifying the parameters of the getperiod function you can make that function far more flexible.
function getperiod( $start, $interval='P1D', $days=7 ){
return new DatePeriod(
new DateTime( $start ),
new DateInterval( $interval ),
new DateTime( date( DATE_COOKIE, strtotime( $start . '+ '.$days.' days' ) ) )
);
}
$start='2018-01-07';
$days=array();
$period=getperiod( $start );
foreach( $period as $date ){
$days[]=$date->format('Y-m-d');
}
echo '<pre>',print_r($days,true),'</pre>';
For instance: To find every Sunday for the next year
$period=getperiod( $start,'P7D', 365 );
foreach( $period as $date ){
$days[]=$date->format('Y-m-d');
}
echo '<pre>',print_r($days,true),'</pre>';
To ensure that the calculations begin on a Sunday which has a numeric value of 7
function getperiod( $start, $interval='P1D', $days=7 ){
return new DatePeriod(
new DateTime( $start ),
new DateInterval( $interval ),
new DateTime( date( DATE_COOKIE, strtotime( $start . '+ '.$days.' days' ) ) )
);
}
/* A date from which to begin calculations */
$start='2018-01-01';
/* Array to store output */
$days=array();
/* integer to represent which day of the week to operate upon */
$startday = 7;
/* Output format for resultant dates */
$output='Y-m-d';
/* Calculate initial startdate given above variables */
$start=date( DATE_COOKIE, strtotime( $start . ' + ' . ( $startday - date( 'N', strtotime( $start ) ) ) . ' days' ) );
/* Get the period range */
$period=getperiod( $start );
foreach( $period as $date ){
/* store output in desired format */
$days[]=$date->format( $output );
}
/* do something with data */
echo '<pre>',print_r($days,true),'</pre>';
From source,
Here is the snippet you are looking for,
// set current date
$date = '01/03/2018';
// parse about any English textual datetime description into a Unix timestamp
$ts = strtotime($date);
// calculate the number of days since Monday
$dow = date('w', $ts);
$offset = $dow - 1;
if ($offset < 0) {
$offset = 6;
}
// calculate timestamp for the Monday
$ts = $ts - $offset*86400;
// loop from Monday till Sunday
for ($i = 0; $i < 7; $i++, $ts += 86400){
print date("m/d/Y l", $ts) . "\n";
}
Here is working demo.
If you need normal standard format code,
Here is your snippet,
// set current date
$date = '2018-01-03';
// parse about any English textual datetime description into a Unix timestamp
$ts = strtotime($date);
// calculate the number of days since Monday
$dow = date('w', $ts);
$offset = $dow - 1;
if ($offset < 0) {
$offset = 6;
}
// calculate timestamp for the Monday
$ts = $ts - $offset * 86400;
// loop from Monday till Sunday
for ($i = 0; $i < 7; $i++, $ts += 86400) {
print date("Y-m-d l", $ts) . "\n";
}
Here is working demo.
EDIT
As per your requirement, now week will start from sunday to saturday
<?php
// set current date
$date = '2018-01-03';
// parse about any English textual datetime description into a Unix timestamp
$ts = strtotime($date);
// calculate the number of days since Sunday
$dow = date('w', $ts);
$offset = $dow;
if ($offset < 0) {
$offset = 6;
}
// calculate timestamp for the Sunday
$ts = $ts - $offset * 86400;
// loop from Sunday till Saturday
for ($i = 0; $i < 7; $i++, $ts += 86400) {
print date("Y-m-d l", $ts) . "\n";
}
<?php
$days = array();
$ddate = "2018-01-07";
$y = date("Y", strtotime($ddate));
if(date("l", strtotime($ddate))=='Sunday'){
$ddate = date("Y-m-d ", strtotime($ddate. "+1 day"));
}
$date = new DateTime($ddate);
$week = $date->format("W");
echo "<br/>";
echo "Weeknummer: $week"."<br>";
echo "Year: $y"."<br>";
for($day=0; $day<=6; $day++)
{
$days[$day] = date('Y-m-d', strtotime($y."W".$week.$day))."<br>";
}
print_r($days);
?>
try this:
$days = array();
$ddate = "2018-01-07";
$date = new DateTime($ddate);
$week = $date->format("N")==7?$date->modify("+1 week")->format("W"):$date->format("W");
$y = date("Y", strtotime($ddate));
echo "Weeknummer: $week"."<br>";
echo "Year: $y"."<br>";
for($day=0; $day<=6; $day++)
{
$days[$day] = date('Y-m-d', strtotime($y."W".$week.$day))."<br>";
}
print_r($days);
$dto = new DateTime();
$year = date_create($this->week_date)->format('o');
$week_no = date('W', strtotime($this->week_date));
$dto->setISODate($year, $week_no);
$dto->modify('-1 days');
$ret['sunday'] = $dto->format('Y-m-d');
$dto->modify('+1 days');
$ret['monday'] = $dto->format('Y-m-d');
$dto->modify('+1 days');
$ret['tuesday'] = $dto->format('Y-m-d');
$dto->modify('+1 days');
$ret['wednesday'] = $dto->format('Y-m-d');
$dto->modify('+1 days');
$ret['thursday'] = $dto->format('Y-m-d');
$dto->modify('+1 days');
$ret['friday'] = $dto->format('Y-m-d');
$dto->modify('+1 days');
$ret['saturday'] = $dto->format('Y-m-d');
$dto->modify('+1 days');
$ret['next_sunday'] = $dto->format('Y-m-d');

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

Error during third saturday date fetching

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

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