i'm creating a calendar in php, but i can't start it on the saturday, i mean i would like a table that begins on saturday and finishes the following friday. i'm stuck :(
echo "<table class='table tableDisplay".$currentMonthNumber." table-bordered '>";
echo "<tr>
<td>Saturday</td>
<td>Sunday</td>
<td>Monday</td>
<td>Tuesday</td>
<td>Wednesday</td>
<td>Thursday</td>
<td>Friday</td>
</tr><tr>";
for ($i=1; $i <= $nbOfDaysInCurrentMonth ; $i++) {
$p = date('w', mktime(0,0,0, $calendrier_date_mois, $i, $calendrier_date_annee));
if($p == 5){
echo "</tr><tr>";
}
echo "<td>".$i."</td>";
}
echo "</table>";
You can use PHP's DateTime API. I have created a sample script for you below, where the script first checks if today is saturday, if not then it will use last saturday and then loops through 7 days.
<?php
$firstDay = null;
$isSaturday = (new DateTime("NOW"))->format('D') == 'Sat';
if($isSaturday) {
$firstDay = new DateTime("NOW");
} else {
$firstDay = new DateTime("last Saturday");
}
$days = 7;
for($i=0;$i<$days;$i++) {
echo $firstDay->format('Y-m-d').'<br />';
$firstDay->modify('+1 day');
}
Try this,
// Save for common reference
$today = strtotime('today');
// Get the first and last day of this month
$fdotm = strtotime('first day of this month', $today);
$ldotm = strtotime('last day of this month', $today);
// first cell (top left) of the calendar
$first = date('w', $fdotm) == 6? $fdotm: strtotime('last saturday', $fdotm);
// last cell (bottom right) of the calendar
$last = strtotime('next friday', $ldotm);
// Rendering
echo '<h2>', date('F Y', $today), '</h2>';
echo '<table>
<tr>
<th>SAT</th>
<th>SUN</th>
<th>MON</th>
<th>TUE</th>
<th>WED</th>
<th>THU</th>
<th>FRI</th>
</tr>';
for ($date = $first; $date <= $last; $date = strtotime('tomorrow', $date)){
if (date('w', $date) == 6) echo "\n<tr>\n";
$fmt = $date == $today? '<b>%s</b>': '%s';
echo '<td>', sprintf($fmt, date('j', $date)), "</td>\n";
if (date('w', $date) == 5) echo "</tr>";
}
echo "\n</table>";
Hope this helps.
Related
I want to display dates between two dates in foreach loop.
Suppose the date starts May 1 2014 and ends May 3, 2014 .I want to display like this
//With for loop alone this will display what I wanted
for($i=$dateFrom;$i<=$dateTo;$i++) {
echo $i."<br>";
}
May 1,2014
May 2, 2014
May 3, 2014
But this code returns when using foreach loop to display it is template
May 3,2014
May 3, 2014
May 3, 2014
This is my code in action.php
$dateTo = $r->getParameter("date_to", date('Y-m-d'));
$dateFrom = $r->getParameter("date_from", date('Y-m-d', strtotime('7 days ago', strtotime($dateTo))));
$this->data=array();
foreach($this->deposits as $d) {
$obj = new stdClass();
$obj->created_by = $d->created_by;
$obj->date_created = $d->date_created;
for($i =$dateFrom;$i <= $dateTo;$i++) {
$date=$i;//I think the problem is here.I am not sure how to make this work
}
$obj->amount = $d->amount;
$obj->dateCovered = $date;
$this->data[] = $obj;
}
template.php
<?php foreach ($data as $i => $d): ?>
<tr class="<?php echo ($i%2==0)?'even':'odd' ?>">
<td><?php echo $d->id ?></td>
<td>
<?php echo $d->dateCovered ?>//this will display the last date only
</td>
<td>
<?php $amountTotal += $d->amount ?>
PHP <?php echo number_format($d->amount, 2) ?>
</td>
<td><?php echo $d->created_by ?></td>
<td><?php echo date("F d, Y",strtotime($d->date_created)) ?></td>
</tr>
<?php endforeach ?>
My problem here is it does not display all dates between two dates.Instead it will only display the last date between two dates.What's wrong with this code?
you can do like this
$date_from = strtotime("1 May 2015");
$date_to = strtotime("15 May 2015");
$oneDay = 60*60*24;
for($i=$date_from; $i<=$date_to; $i=$i+$oneDay)
{
echo date("F j, Y", $i) . "<br>";
}
Now you can try with this..
$dateTo = $r->getParameter("date_to", date('Y-m-d'));
$dateFrom = $r->getParameter("date_from", date('Y-m-d', strtotime('7 days ago', strtotime($dateTo))));
$oneDay = 60*60*24;
$date_time = strtotime($dateFrom);
$this->data=array();
foreach($this->deposits as $d) {
$obj = new stdClass();
$obj->created_by = $d->created_by;
$obj->date_created = $d->date_created;
$date= date("F j, Y", $date_time);
$date_time += $oneDay;
$obj->amount = $d->amount;
$obj->dateCovered = $date;
$this->data[] = $obj;
}
<?php
$date_from = strtotime("10 September 2000");
$date_to = strtotime("15 September 2000");
$day_passed = ($date_to - $date_from); //seconds
$day_passed = ($day_passed/86400); //days
$counter = 1;
$day_to_display = $date_from;
while($counter < $day_passed){
$day_to_display += 86400;
echo date("F j, Y \n", $day_to_display);
$counter++;
}
?>
I want to produce a form that has a dropdown options box containing dates in order and in a format but I have no idea how to display them as a string.
I assume that you need to do this sort of thing to get the start and finish but how can I get the dates for each "Option"?
<?php
date_default_timezone_set('UTC');
$start = date('Ymd');
$end = date('Ymd', strtotime('+60 days'));
while (strtotime($start) <= strtotime($end)) {
echo "$start\n";
$date = date ("Ymd", strtotime("+1 day", strtotime($start)));
}
?>
$dates = array();
$dates[] = time();
for ($i = 1; $i < 60; $i++) { $dates[] = strtotime("+{$i} days"); }
$html = NULL;
foreach ($dates as $date) { $dates .= "<option value='" . date('Ymd', $date) . "'>" . date('d F Y', $date) . "</option>"; }
$html = "<select id='dates'>{$dates}</select>";
echo $html;
I would just like to add to Publi Design's answer to make the answer more specific to the question.
PHP courtesy of Publi Design:
// Start date
$date = '2009-12-06';
// End date
$end_date = '2020-12-31';
?>
<select id="dates">
<?php while (strtotime($date) <= strtotime($end_date)) { ?>
<option name="date"><?php echo (string)$date; ?></option>
<?php $date = date ("Y-m-d", strtotime("+1 day", strtotime($date)));
}?>
</select>
Using a basic PHP while loop:
// Set timezone
date_default_timezone_set('UTC');
// Start date
$date = '2009-12-06';
// End date
$end_date = '2020-12-31';
while (strtotime($date) <= strtotime($end_date)) {
echo "$date\n";
$date = date ("Y-m-d", strtotime("+1 day", strtotime($date)));
}
Original source: here
This might be what you're looking for..
<select id="dates">
<option></option>
<?php
$curr_date = date('YYMMDD');
for ($i = 0; $i < 60; $i++) {
$date = strtotime("+$i day", $curr_date);
echo '<option name="date" value="'.$date.'">'.date('DD m YY', strtotime($date)).'</option>'."\n";
}
?>
</select>
I have created a php calender which will show one week at a time.Here is the code i have created
<?php
$week = date("W");
$year = (isset($_GET['year']))?$_GET['year']:date("Y");
$week = (isset($_GET['week']))?$_GET['week']:Date('W');
if($week>53){
$year+= 1;
$week=1;
}
?>
Next Week <!--Next week-->
Pre Week <!--Previous week-->
<table border="1px">
<tr>
<td>Employee</td>
<?php
for($day=1; $day<=7; $day++)
{
$d = strtotime($year."W".$week.$day);
echo "<td>".date('l',$d )."<br>";
echo date('d M',$d)."</td>";
}
?>
</tr>
when i am trying to go to the next week it is working correctly. But when the year is changing it is not working for the next year.
Leave the week calculation to the DateTime::setIsoDate() method.
Here is the simplest and best solution for your problem :
<?php
$dt = new DateTime;
if (isset($_GET['year']) && isset($_GET['week'])) {
$dt->setISODate($_GET['year'], $_GET['week']);
} else {
$dt->setISODate($dt->format('o'), $dt->format('W'));
}
$year = $dt->format('o');
$week = $dt->format('W');
?>
Pre Week <!--Previous week-->
Next Week <!--Next week-->
<table>
<tr>
<td>Employee</td>
<?php
do {
echo "<td>" . $dt->format('l') . "<br>" . $dt->format('d M Y') . "</td>\n";
$dt->modify('+1 day');
} while ($week == $dt->format('W'));
?>
</tr>
</table>
When you're using strtotime, your week has to be two-digit. You have to prepend a zero, if the week is lower than 10 before the for loop.
if($week < 10) {
$week = '0'. $week;
}
for($day = 1; $day <= 7; $day++) {
Also, a year only has 52 weeks, the condition at the beginning should be.
if($week > 52) {
$year++;
$week = 1;
} elseif($week < 1) { // If you want the possibility to go back too
$year--;
$week = 52;
}
Full code:
<?php
$year = (isset($_GET['year'])) ? $_GET['year'] : date("Y");
$week = (isset($_GET['week'])) ? $_GET['week'] : date('W');
if($week > 52) {
$year++;
$week = 1;
} elseif($week < 1) {
$year--;
$week = 52;
}
?>
Next Week <!--Next week-->
Pre Week <!--Previous week-->
<table border="1px">
<tr>
<td>Employee</td>
<?php
if($week < 10) {
$week = '0'. $week;
}
for($day= 1; $day <= 7; $day++) {
$d = strtotime($year ."W". $week . $day);
echo "<td>". date('l', $d) ."<br>". date('d M', $d) ."</td>";
}
?>
</tr>
</table>
I made a simple PHP script for generating calendar. It has rounded weeks (I mean if month starts on Friday it will generate it monday of that week), navigation, etc. Works great, but there is a bug in October. It draws last sunday of month twice. I use sundays as a singnal for new row so it makes
Example of my calendar
Example of calendar with October bug
And here the code (I'm not professional, I learned PHP on my own, with no books, just with google and PHP manual):
<?php
function getfirstday($month, $year)
{
$datestr = "01-$month-$year";
$day = date('N', strtotime($datestr));
return $day;
}
function getlastday($month, $year)
{
$datestr = cal_days_in_month(CAL_GREGORIAN, $month, $year)."-$month-$year";
$day = date('N', strtotime($datestr));
return $day;
}
//Don't care about this, I just want to have weekdays in my primary language
function getweekday($weekDay) {
$list = array();
$list['1'] = "Pondělí";
$list['2'] = "Úterý";
$list['3'] = "Středa";
$list['4'] = "Čtvrtek";
$list['5'] = "Pátek";
$list['6'] = "Sobota";
$list['7'] = "Neděle";
return $list[$weekDay];
}
//What month and year do we want to show?
//Ger it from URL or use current
$month = $_GET['month'];
if ($month == "") {
$month = date('m');
}
$year = $_GET['year'];
if ($year == "") {
$year = date('Y');
}
//Firts day of month
$startDayStr = "01-$month-$year";
//Some calculation to get interval of whole month and rounded weeks
$startDay = strtotime($startDayStr) - (getfirstday($month, $year ) - 1) * 24*60*60;
$roundMonth = 7 - getlastday($month, $year );
$limit = cal_days_in_month(CAL_GREGORIAN, $month, $year ) + (getfirstday($month, $year ) - 1) + $roundMonth;
//Some navigation
?>
<table>
<tr>
<?php
if ($month > 1) {
$prevm = $month - 1;
$prevh = "cal.php?month=$prevm&year=$year";
} elseif ($month == 1) {
$prevm = 12;
$prevy = $year -1;
$prevh = "cal.php?month=$prevm&year=$prevy";
}
if ($month < 12) {
$nextm = $month +1;
$nexth = "cal.php?month=$nextm&year=$year";
} elseif ($month == 12) {
$nextm = 1;
$nexty = $year +1;
$nexth = "cal.php?month=$nextm&year=$nexty";
}
?>
<td width="200" align="left"><Previous month</td>
<td width="190" align="center">
<?php echo date('m', strtotime("01-$month-$year 00:00:00")); ?>
</td>
<td width="200" align="right">Next month></td>
</tr>
</table>
<?php
//Let's generate our calendar
echo "<table border=\"1\"><tr>";
for($j=1;$j<=7;$j++){
echo "<td align=\"center\" width=\"85\" height=\"50\"><b>".getweekday($j)."</b></td>";
}
echo "</tr><tr>";
for($i = 1;$i <= $limit;$i++) {
$lastDayOfMonth = strtotime(cal_days_in_month(CAL_GREGORIAN, $month, $year)."-$month-$year 23:59:59");
$firstDayOfMonth = strtotime("01-$month-$year");
$weekDay = date('N', $startDay);
if ($startDay < $firstDayOfMonth || $startDay > $lastDayOfMonth) {
$class = "caltdb";
} else {
$class = "caltda";
}
echo "<td class=\"$class\" align=\"center\" height=\"40\">". date('d', $startDay) ."</td>";
if ($weekDay == '7') {
echo "</tr><tr>";
}
$startDay = $startDay + 24*60*60;
}
echo "</tr></table>";
?>
Could you help me fix this problem? I dont know why is it happening.
Thank you a lot,
Heretiiik
The problem is likely due to the fact that you're using + 24*60*60 to add one day to a timestamp. This causes problems with daylight saving time, because there are days with 23 or 25 hours when DST begins/ends.
Near the end of your script, replace:
$startDay = $startDay + 24*60*60;
with:
$startDay = strtotime('+1 day', $startDay);
I'm just getting started on my PHP journey and was completing a tutorial for the creation of a simple calendar. I'm running into a syntax error in Codepad and haven't been able to find a fix. I'm sure it's something simple that I'm not seeing. Sorry about the notes, I've been trying to annotate as much as possible so I don't get lost.
The error is:
Parse error: syntax error, unexpected ',' on line 10. (the $day=('d', $date) declaration)
Code:
<?php
// current date variable
$date = time ();
//day, month and year variables
$day = ('d', $date);
$month = ('m', $date);
$year = ('Y', $date);
// first day of the month
$monthfirstday = mktime(0,0,0,$month, 1, $year);
// get the name of the month
$monthtitle = ('F', $monthfirstday);
// first day of the week
$weekday = ('D', $monthfirstday);
// identify the days of the week
switch ($weekday) {
case"Sun": $blank=0;
break;
case"Mon": $blank=1;
break;
case"Tue": $blank=2;
break;
case"Wed": $blank=3;
break;
case"Thu": $blank=4;
break;
case"Fri": $blank=5;
break;
case"Sat": $blank=6;
break;
}
// number of days in the month
$daysinmonth = cal_days_in_month(0, $month, $year);
// include the html
echo "<div id='calendar-wrap'>";
echo "<table border=6 width=394><tr><th colspan=60> $monthtitle $year</th></tr>";
echo "
<tr>
\n\t\t<td width=62>SUN</td>
\n\t\t<td width=62>MON</td>
\n\t\t<td width=62>TUES</td>
\n\t\t<td width=62>WEDS</td>
\n\t\t<td width=62>THURS</td>
\n\t\t<td width=62>FRI</td>
\n\t\t<td width=62>SAT</td>
</tr>
";
$daycount = 1;
echo "<tr>";
// dealing with the days of the month
$blank > 0
{
echo "<td></td>";
$blank = $blank-1;
$daycount++;
}
// set the day number to 1
$daynumber = 1;
// count the days of the month
while
( $daynumber <= $daysinmonth )
{
echo "<td> $daynumber </td>";
// increase the day count until the month ends
$daynumber++;
$daycount++;
// add a new row every 7 days
if ($daycount > 7)
{
echo "</tr><tr>";
$daycount = 1;
}
}
// fill in blank days if necessary
while
($daycount > 1 && $daycount <= 7)
{
echo "<td> </td>";
$daycount++;
}
echo "</tr></table></div>";
?>
Thanks in advance,
Mike
Your function is missing!
$day = date('d', $date);
Here you go...
$day = date('d', $date);
$month = date('m', $date);
$year = date('Y', $date);
I think you're trying to do this:
$day = date('d', $date);
This is the Error. Missed the Function :)
$day = date('d', $date);
$month = date('m', $date);
$year = date('Y', $date);