Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Thursday, December 17, 2015

get all days and dates for the selected month of a year

get all days and dates for the selected month of a year


how to get both days and dates for selected month of selected year and show in tables. e-g: i have tried so far.

";  print_r($dates);  echo "
";*/ ?> ".$date.""; } ?>

this executes for me this code.

010203040506070809101112131415161718192021222324252627282930

but i want another row below the dates row. which should show day related to that date??

by day i mean :monday, tues, wed etc. by date i mean : 1, 2, 3, 4 etc

so it would be like

1234  MonTuesWedThursday  

I hope i could explain my self..

Answer by Ghost for get all days and dates for the selected month of a year


You can use date('l') to get the corresponding day name:

  $day_num 
$day_name"; } ?>

Answer by Peter for get all days and dates for the selected month of a year


A different approach could be to use the DateTime object:

$aDates = array();  $oStart = new DateTime('2014-12-01');  $oEnd = clone $oStart;  $oEnd->add(new DateInterval("P1M"));    while ($oStart->getTimestamp() < $oEnd->getTimestamp()) {      $aDates[] = $oStart->format('D d');      $oStart->add(new DateInterval("P1D"));  }  

Then to print:

foreach ($aDates as $day) {      echo $day;  }  

For more information about the format parameters, you can refer to: http://php.net/manual/en/function.date.php

Answer by Alex for get all days and dates for the selected month of a year


In your case date('D', strtotime($date)) should work, but you need the date to be in format yyyy-mm-dd

I have done some tests so result:

for( $i=1; $i<= $num_of_days; $i++){      $dates[]= str_pad($i,2,'0', STR_PAD_LEFT);      $d = "2003-09-".$i;      $days[] = date('D', strtotime($d));  }  

Added another tr for days:

  ".$day."";  }  ?>    

Answer by Ram Sharma for get all days and dates for the selected month of a year


You can try something like this

$myYearMonth = '2003-09';    $start = new DateTime(date('Y-m-01', strtotime($myYearMonth)));  $end = new DateTime(date('Y-m-t', strtotime($myYearMonth)));    $diff = DateInterval::createFromDateString('1 day');  $periodStart = new DatePeriod($start, $diff, $end);    foreach ( $periodStart as $dayDate ){    echo ''.$dayDate->format( "d\n" ).''.$dayDate->format( "l\n" ).'';  }    

Answer by Adam for get all days and dates for the selected month of a year


You shouldn't use the date() function as the date range is limited to 1970-2038 according to the PHP documentation :

The valid range of a timestamp is typically from Fri, 13 Dec 1901 20:45:54 GMT to Tue, 19 Jan 2038 03:14:07 GMT. (These are the dates that correspond to the minimum and maximum values for a 32-bit signed integer). However, before PHP 5.1.0 this range was limited from 01-01-1970 to 19-01-2038 on some systems (e.g. Windows).

Using the DateTime() class you can retrieve the information you need for a given month using the following function. This uses DateTime::format() to get the day name in English (no localization).

function getMonth($year, $month) {        // this calculates the last day of the given month      $last=cal_days_in_month(CAL_GREGORIAN, $month, $year);        $date=new DateTime();      $res=Array();        // iterates through days      for ($day=1;$day<=$last;$day++) {              $date->setDate($year, $month, $day);                $res[$day]=$date->format("l");      }      return $res;  }  

This will return an associated array like the following:

$res=getMonth(2015, 2);  print_r($res);      Array  (  [1] => Sunday  [2] => Monday  [3] => Tuesday  [4] => Wednesday  [5] => Thursday    [...]    )  

To output the data in a two-row table, you can use the following code:

'.implode('', array_keys($res)).'';  echo ''.implode('', $res).'';  

As the Datetime::format() function does not support locales for translation, you can use an associative array to get the translation in another language.

Answer by Christian for get all days and dates for the selected month of a year


A DateTime method that builds an assoc array of date -> day.

The results for December 2015 look like this:

array(31) {      [1] = string(7) "Tuesday"      [2] = string(9) "Wednesday"      [3] = string(8) "Thursday"      [4] = string(6) "Friday"      [5] = string(8) "Saturday"      [6] = string(6) "Sunday"      [7] = string(6) "Monday"      [8] = string(7) "Tuesday"      [9] = string(9) "Wednesday"      [10] = string(8) "Thursday"      [11] = string(6) "Friday"      [12] = string(8) "Saturday"      [13] = string(6) "Sunday"      [14] = string(6) "Monday"      [15] = string(7) "Tuesday"      [16] = string(9) "Wednesday"      [17] = string(8) "Thursday"      [18] = string(6) "Friday"      [19] = string(8) "Saturday"      [20] = string(6) "Sunday"      [21] = string(6) "Monday"      [22] = string(7) "Tuesday"      [23] = string(9) "Wednesday"      [24] = string(8) "Thursday"      [25] = string(6) "Friday"      [26] = string(8) "Saturday"      [27] = string(6) "Sunday"      [28] = string(6) "Monday"      [29] = string(7) "Tuesday"      [30] = string(9) "Wednesday"      [31] = string(8) "Thursday"  }  

The full code to get your desired table:

format('F');       // Prepare results array     $results = array();       // While same month     while($start->format('F') == $month){        // Add to array        $day              = $start->format('l');        $date             = $start->format('j');        $results[$date]   = $day;          // Next Day        $start->add(new DateInterval("P1D"));     }       // Return results     return $results;  }  ?>        

Answer by Glufu for get all days and dates for the selected month of a year


Try this script:

// Day of month, e.g. 2014-12-14 if you need the table for december 2014  $date = time();    // Array containing the dates and weekdays  $days = array();  // loop to populate the array  for(      $day = strtotime('midnight', strtotime(date('1 F Y', $date)));   /* first day of month */      $day < strtotime(date('1 F Y', strtotime('next month', $date))); /* first day of next month */      $day = strtotime('next day', $day)  ){      // insert current day into the array      $days[date('d', $day)] = date('l', $day);  }    // print the row containing all day numbers  echo ''.implode('', array_keys($days)).'';  // print the row containing all weekday names  echo ''.implode('', $days).'';  

Answer by mark for get all days and dates for the selected month of a year


Full solution below.

I determine the number of days in the given month/year combination. Then I loop over the days and create the two required rows at the same time.

When done, the two rows are wrapped in a table and returned to the caller.

    ".sprintf('%02d', $i)."";              $resultDays .= "".date("l", mktime(0, 0, 0, $month, $i, $year))."";          }          // return the result wrapped in a table          return "".PHP_EOL.          "".$resultDate."".PHP_EOL.          "".$resultDays."".PHP_EOL.          "
"; } ?>

phpfiddle link: http://phpfiddle.org/main/code/ffjm-hqsu


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 71

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.