PHP Beginner...Simple PHP syntax error - php

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

Related

Calculate days in months between two dates with PHP

I have a period with startdate of 2016-12-26 and end date 2017-03-04.
Now I would like to find out how many days in each months there is, from a given period. Expected output from the above period dates (array):
2016-12: 5
2017-01: 31
2017-02: 28
2017-03: 4
How can I accomplish this cleanest way? I have tried to:
first looking at the period_start, get the days = 26 and
find out the start/end dates of the months between 2016-12 and 2017-03, to then calculate the days here (31 respectively 28 in february)
then finally calculating the 4 days in 2017-03.
But is there any cleaner/better way?
This can be achieved easily using the DateTime class. Create the objects, and use DateTime::diff() on them, then use the days property.
$start = new DateTime("2016-12-26");
$end = new DateTime("2017-03-04");
echo $start->diff($end)->days; // Output: 68
Live demo
http://php.net/datetime.construct
#Karem hope this logic will help you, this is working case for all your conditions please try this below one:
<?php
$startDate = '2016-12-26';
$endDate = '2017-03-04';
$varDate = $startDate;
while($varDate < $endDate){
$d = date('d', strtotime($varDate));
$Y = date('Y', strtotime($varDate));
$m = date('m', strtotime($varDate));
$days = cal_days_in_month(CAL_GREGORIAN,$m,$Y);
$time = strtotime($varDate);
if($varDate == $startDate){
$time = strtotime(date('Y-m-01', $time));
$days = $days - $d;
}
else if(date("Y-m", strtotime($varDate)) == date("Y-m", strtotime($endDate))){
$days = date("j", strtotime($endDate));
}
echo date('Y-m', strtotime($varDate)). ": ".$days."<br>";
$varDate = date('Y-m-d', strtotime("+1 month", $time));
}
This is long but easy to understand that how to achieve you your goal
<?php
function getMonthDays($start,$end){
if($start < $end){
$start_time = strtotime($start);
$last_day_of_start = strtotime(date("Y-m-t",$start_time));
$start_month_days = ($last_day_of_start - $start_time)/(60*60*24);
echo date("Y-m",$start_time).": ".$start_month_days."\n";
$days = "";
$start = date("Y-m-d", strtotime("+1 month", $start_time));
$start_time = strtotime($start);
while($start < $end){
$month = date("m",$start_time);
$year = date("Y",$start_time);
$days = date('t', mktime(0, 0, 0, $month, 1, $year));
echo date("Y-m",$start_time).": ".$days."\n";
$start = date("Y-m-d", strtotime("+1 month", $start_time));
$start_time = strtotime($start);
}
echo date("Y-m",strtotime($end)).": ".date("d",strtotime($end))."\n";
}else{
echo "Wrong Input";
}
}
getMonthDays('2016-12-26','2017-03-04');
?>
live demo : https://eval.in/781724
Function returns array : https://eval.in/781741
<?php
$d1 = strtotime('2016-12-26');
$d2 = strtotime('2017-03-04');
echo floor(($d2 - $d1)/(60*60*24));
?>
i used Carbon (https://carbon.nesbot.com/docs/) but you can do it with any other time lib.
$startDate = Carbon::createFromFormat('!Y-m-d', '2017-01-11');;
$endDate = Carbon::createFromFormat('!Y-m-d', '2018-11-13');;
$diffInMonths = $endDate->diffInMonths($startDate);
for ($i = 0; $i <= $diffInMonths; $i++) {
$start = $i == 0 ? $startDate->copy()->addMonth($i) : $startDate->copy()->addMonth($i)->firstOfMonth();
$end = $diffInMonths == $i ? $endDate->copy() : $start->copy()->endOfMonth();
echo $end->format('Y-m') . ' ' . ($end->diffInDays($start) + 1) . PHP_EOL;
}

Function for selecting next Day If the last day is weekend

I am trying to create a function that checks the range of date. If the last date is an weekend it will select the next day. Suppose, Today is 1 March 2016. If someone add 5 with it, it will make the result 7 March 2016. Because 6 March is Weekend. I have written this code. But it is not working and I cannot find where is the problem. The code is as below:
<?php
$date = "2016-02-29";
$numOfDays = 8;
$futureDate = strtolower(date("l",strtotime($date." +".$numOfDays."days")));
$weekend1= "friday";
$weekend2= "saturday";
if($futureDate != $$w1 || $futureDate != $w2){
$finalDate = date("Y-m-d",strtotime($futureDate));
}
else{$finalDate = date("Y-m-d",strtotime($futureDate ."+1 day"));}
echo $finalDate;
?>
Check this:
<?php
$date = "2016-02-29";
$numOfDays = 11;
$futureDate = strtolower(date("l",strtotime($date ."+".$numOfDays."days")));
$futureDate1 = strtolower(date("Y-m-d",strtotime($date ."+".$numOfDays."days")));
$weekend1= "friday";
$weekend2= "saturday";
if($futureDate == $weekend1){
$finalDate1 = date("Y-m-d",strtotime($futureDate1 ."+2 days"));
}
if ($futureDate == $weekend2){
$finalDate1 = date("Y-m-d",strtotime($futureDate1 ."+1 days"));
}
echo $finalDate1;
?>
March 6th, 2016 is a Sunday, but you're checking for Friday and Saturday, you need to check for Saturday and Sunday instead, for the weekend. Also, your variable names need to match, and for Saturday you'll need to add two days to get the next weekday.
<?php
$date = "2016-02-29";
$numOfDays = 8;
$day = 86400;//one day is 86400 seconds
$time = strtotime($futureDate + $numOfDays * $day);//converts $futureDate to a UNIX timestamp
$futureDate = strtolower(date("l", $time));
$saturday = "saturday";
$sunday = "sunday";
if($futureDate == $saturday){
$finalDate = date("Y-m-d", $time + 2 * $day);
}
else if($futureDate == $sunday){
$finalDate = date("Y-m-d", $time + $day);
}
else{
$finalDate = date("Y-m-d", $time);
}
echo($finalDate);
?>
#Fakhruddin Ujjainwala
I have changed in your code and it works now perfect. Thanks. the code is now as below:
<?php
$date = "2016-02-29";
$numOfDays = 4;
$futureDate = strtolower(date("l",strtotime($date ."+".$numOfDays."days")));
$futureDate1 = strtolower(date("Y-m-d",strtotime($date ."+".$numOfDays."days")));
$weekend1= "friday";
$weekend2= "saturday";
if($futureDate == $weekend1){
$finalDate1 = date("Y-m-d",strtotime($futureDate1 ."+2 days"));
}
else if ($futureDate == $weekend2){
$finalDate1 = date("Y-m-d",strtotime($futureDate1 ."+1 days"));
}
else{
$finalDate1 = date("Y-m-d",strtotime($futureDate1));
}
echo $finalDate1;
?>

Get the same specific date on each month of the whole year

I am working on a "cut-off" date and I need to set it on every month. Say, I set the cut-off date to August 25 for this year, then it should be September 25, October 25 and so on till the year ends.
Here's the code I have:
$now = "2015-08-25";
$nextDate = getCutoffDate($now);
echo $nextDate;
function getCutoffDate($start_date)
{
$date_array = explode("-",$start_date); // split the array
// var_dump($date_array);
$year = $date_array[0];
$month = $date_array[1];
$day = $date_array[2];
/*if (date('n', $now)==12)
{
return date("Y-m-d",strtotime(date("Y-m-d", $start_date) . "+1 month"));
}*/
if (date("d") <= $day) {
$billMonth = date_format(date_create(), 'm');
}
else{
$billMonth = date_format(date_modify(date_create(), 'first day of next month'), 'm');
}
// echo date("d").' '. $billMonth.'<br>';
$billMonthDays = cal_days_in_month(CAL_GREGORIAN, ($billMonth), date("Y"));
// echo $billMonthDays.'<br>';
if ($billMonthDays > $day) {
$billDay = $day;
} else {
$billDay = $billMonthDays;
}
}
I got this from here: http://www.midwesternmac.com/blogs/jeff-geerling/php-calculating-monthly
It returns the same date for the next month only, but how do I get the specific date of EACH month of the current year? Kindly leave your thoughts. Still a newbie here, sorry.
In that case, this should be enough:
<?php
for($i=8; $i<=12; $i++) {
echo sprintf('25-%02d-2015', $i) . '<br>';
}
but if you need more flexible way:
<?php
$date = new DateTime('25-08-2015');
function getCutoffDate($date) {
$days = cal_days_in_month(CAL_GREGORIAN, $date->format('n'), $date->format('Y'));
$date->add(new DateInterval('P' . $days .'D'));
return $date;
}
for($i = 0; $i < 5; $i++) {
$date = getCutoffDate($date);
echo $date->format('d-m-Y') . '<br>';
}
This should print:
25-09-2015
25-10-2015
25-11-2015
25-12-2015
25-01-2016

My PHP script for generating calendar bugs in october

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

PHP to calculate latest 31 March

i want to calculate latest 31-Mar .... suppose date is 1-Jan-2012 i want result as 31-mar-2011 and if is 1-April-2011 then also i want result 31-mar-2011 and if its 1-mar-2011 it should come as 31-mar-2010.....hope i made my self clear ...(with php) ... i al calculating date with this for financial year ...
always between 31-mar-lastyear to 1-April-thisyear
... year should be taken automatically ...
i was trying like this
31-mar-date('y') and 31-mar-date('y')-1
but its not working as its taking current year every time.
Here is an example using the wonderful strtotime function of php.
<?php
$day = 1;
$month = 1;
$year = 2011;
$date = mktime(12, 0, 0, $month, $day, $year);
$targetMonth = 3;
$difference = $month - $targetMonth;
if($difference < 0) {
$difference += 12;
}
$sameDateInMarch = strtotime("- " . $difference . " months", $date);
echo "Entered date: " . date("d M Y", $date) . "<br />";
echo "Last 31 march: " . date("t M Y", $sameDateInMarch);
// the parameter 't' displays the last day of the month
?>
Something like this:
function getLast() {
$currentYear = date('Y');
// Check if it happened this year, AND it's not in the future.
$today = new DateTime();
if (checkdate(3, 31, $currentYear) && $today->getTimestamp() > mktime(0, 0, 0, 3, 31, $currentYear)) {
return $currentYear;
}
while (--$currentYear) {
if (checkdate(3, 31, $currentYear)) {
return $currentYear;
}
}
return false;
}
var_dump(getLast());
It should return the year or false.
if (date('m')>3) {
$year = date('Y').'-'.(date('Y')+1);
} else {
$year = (date('Y')-1).'-'.date('Y');
}
echo $year;
this is to get the current financial year for India

Categories