I'm currenty looking into this tutorial to create a calendar. The only problem I have atm is that my months are in english instead of dutch. How can I change the output of 'july' to 'juli' ?
<?php
$vandaag = date("d"); // Current day
$maand = date("m"); // Current month
$jaar = date("Y"); // Current year
$dagen = cal_days_in_month(CAL_GREGORIAN,$maand,$jaar); // Days in current month
$vorigemaand = date("t", mktime(0,0,0,$maand-1,1,$jaar)); // Days in previous month
$begin = date("N", mktime(0,0,0,$maand,1,$jaar)); // Starting day of current month
$einde = date("N", mktime(0,0,0,$maand,$dagen,$jaar)); // Finishing day of current month
$vorigestart = $begin - 1; // Days of previous month in calander
$counter = 1;
$volgendeMaandCounter = 1;
if($begin > 5){ $rows = 6; }else {$rows = 5; }
for($i = 1; $i <= $rows; $i++){
echo '<tr class="week">';
for($x = 1; $x <= 7; $x++){
if(($counter - $begin) < 0){
$date = (($vorigemaand - $vorigestart) + $counter);
$class = 'class="blur"';
}else if(($counter - $begin) >= $dagen){
$date = ($volgendeMaandCounter);
$volgendeMaandCounter++;
$class = 'class="blur"';
}else {
$date = ($counter - $begin + 1);
if($vandaag == $counter - $begin + 1){
$class = 'class="today"';
}
}
echo '<td '.$class.'><a class="date">'. $date . '</a></td>';
$counter++;
$class = '';
}
echo '</tr>';
}
?>
Thanks in advance!
try with this code:
setlocale(LC_TIME, 'de_DE', 'deu_deu');
/* print test date string */
echo strftime("%A, %d. %B %Y");
Related
I have some code for a calendar. it works and displays as it should. However, I was wondering is there a way to display years?
At the moment it just shows current year (2018) but i would like for it to have buttons on side to go to next year, 2019 etc..
See code below. Can anyone help?
How about this:
You will need to pass an additional param to the query, in my example: y
With this, you will be able to create links to years like:
/page.php?y=1999&m=4
$monthArg = !empty($_GET['m']) ? intval($_GET['m']) : date('m');
$yearArg = !empty($_GET['y']) ? intval($_GET['y']) : date('Y'); // you will need to add an other query parameter for this
$currYear = $yearArg;
$startDate = strtotime($currYear . "-" . $monthArg . "-01 00:00:01");
$startDay = date("N", $startDate);
$monthName = date("M",$startDate );
$daysInMonth = cal_days_in_month(CAL_GREGORIAN, date("m", $startDate), date( "Y", $startDate));
$endDate = strtotime($currYear . "-" . $monthArg . "-" . $daysInMonth ." 00:00:01");
$endDay = date("N", $endDate);
if ($startDay> 6)
$startDay = 7 - $startDay;
$currElem = 0;
$dayCounter = 0;
$firstDayHasCome = false;
$arrCal = [];
for($i = 0; $i <= 5; $i ++) {
for($j= 0; $j <= 6; $j++) {
// decide what to show in the cell
if($currElem < $startDay && !$firstDayHasCome)
$arrCal[$i][$j] = "";
else if ($currElem == $startDay && !$firstDayHasCome) {
$firstDayHasCome= true;
$arrCal[$i][$j] = ++$dayCounter;
}
else if ($firstDayHasCome) {
if ($dayCounter < $daysInMonth)
$arrCal[$i][$j] = ++ $dayCounter;
else
$arrCal[$i][$j] = "";
}
$currElem ++;
}
}
I want output to be count of sunday's present in given month and year.
This is my code:
$months=$_POST['month'];
$years=$_POST['year'];
$monthName = date("F", mktime(0, 0, 0, $months));
$fromdt=date('Y-m-01 ',strtotime("First Day Of $monthName $years")) . '<br/>';
$todt=date('Y-m-d ',strtotime("Last Day of $monthName $years")) . '<br/>';
$num_sundays='';
for ($i = 0; $i < ((strtotime($todt) - strtotime($fromdt)) / 86400); $i++)
{
if(date('l',strtotime($fromdt) + ($i * 86400)) == 'Sunday')
{
$num_sundays++;
}
}
I am not getting any output if i echo $num_sundays. Please help me . I am new to PHP
You just need to remove <br> from these two lines:
$fromdt=date('Y-m-01 ',strtotime("First Day Of $monthName $years")) . '<br/>';
$todt=date('Y-m-d ',strtotime("Last Day of $monthName $years")) . '<br/>';
Otherwise this will be the part of start and end date, and your strtotime() will return false.
Example:
<?php
$months = 12;
$years=2016;
$monthName = date("F", mktime(0, 0, 0, $months));
$fromdt=date('Y-m-01 ',strtotime("First Day Of $monthName $years")) . '<br/>';
$todt=date('Y-m-d ',strtotime("Last Day of $monthName $years")) . '<br/>';
var_dump(strtotime($todt));
var_dump(strtotime($fromdt));
?>
DEMO: This will return false for both.
Example 2:
<?php
$months = 12;
$years=2016;
$monthName = date("F", mktime(0, 0, 0, $months));
$fromdt=date('Y-m-01 ',strtotime("First Day Of $monthName $years")) ;
$todt=date('Y-m-d ',strtotime("Last Day of $monthName $years"));
var_dump(strtotime($todt));
var_dump(strtotime($fromdt));
?>
DEMO: This will return the values
Complete Example:
<?php
$months = 12;
$years=2016;
$monthName = date("F", mktime(0, 0, 0, $months));
$fromdt=date('Y-m-01 ',strtotime("First Day Of $monthName $years")) ;
$todt=date('Y-m-d ',strtotime("Last Day of $monthName $years"));
$num_sundays='';
for ($i = 0; $i < ((strtotime($todt) - strtotime($fromdt)) / 86400); $i++)
{
if(date('l',strtotime($fromdt) + ($i * 86400)) == 'Sunday')
{
$num_sundays++;
}
}
echo "Total Count is: ".$num_sundays;
?>
DEMO: This will return 4 sunday
Without loop. I hope this gives the correct results.
date_default_timezone_set('UTC');
// unix timestamp 0 = Thursday, 01-Jan-70 00:00:00 UTC
// unix timestamp 259200 = Sunday, 04-Jan-70 00:00:00 UTC
$sun_first = strtotime('1970-01-04');
$t1 = strtotime('2018-10-01') - $sun_first - 86400;
$t2 = strtotime('2018-10-31') - $sun_first;
$sun_count = floor($t2 / 604800) - floor($t1 / 604800); // total Sunday from 2018-10-01 to 2018-10-31
echo $sun_count; // 4
Get all sunday in month see below code:
function total_sun($month,$year)
{
$sundays=0;
$total_days=cal_days_in_month(CAL_GREGORIAN, $month, $year);
for($i=1;$i<=$total_days;$i++)
if(date('N',strtotime($year.'-'.$month.'-'.$i))==7)
$sundays++;
return $sundays;
}
echo total_sun(11,2016);
http://phpio.net/s/l9f
Check following Example. I work perfectly..
function dayCount($day,$month,$year){
$totalDay=cal_days_in_month(CAL_GREGORIAN,$month,$year);
$count=0;
for($i=1;$totalDay>=$i;$i++){
if( date('l', strtotime($year.'-'.$month.'-'.$i))==ucwords($day)){
$count++;
}
}
echo $count;
}
dayCount('saturday',3,2019);
working example dorcode calculation
$startd="31-7-2006 15:30:00";
$endd="31-7-2007 15:30:00";
$startDate=$startd;
$endDate=$endd;
$startDate1 = strtotime($startDate);
$endDate1 = strtotime($endDate);
if($startDate1>$endDate1)
{
$startDate1 = strtotime($endDate);
$endDate1 = strtotime($startDate);
} else {
$startDate1 = strtotime($startDate);
$endDate1 = strtotime($endDate);
}
$p=0;
for($i = strtotime("Sunday", $startDate1); $i <= $endDate1;
$i =strtotime('+1 week', $i))
{
$p++;
echo $p.": ".date('F d, Y', $i)."<br>";
}
To get a count of any given day in a given month in a year:
$year = '2019';
$month = '2';
$day = 'Tuesday';
$count = 0;
$days = cal_days_in_month(CAL_GREGORIAN, $month, $year);
$date = new Datetime($year.'-'.$month.'-01');
for($i=1; $i<$days; $i++){
if($date->format('l') == $day){
$count++;
}
$date->modify('+1 day');
}
echo "Count: $count";
Use this code if it helps you
public function countWeekendDays($month, $year){
$daytime = strtotime(date($year."/".$month."/01 00:00:01"));
$daysOfMonth = date("t", $daytime);
$weekdays = 0;
for ($day=1; $day <= $daysOfMonth; $day++) {
$time = strtotime(date($year.'/'.$month.'/'.$day.' 00:00:01'));
$dayStr = date('l', $time);
if ($dayStr == 'Saturday' || $dayStr == 'Sunday') {
$weekdays++;
}
}
return $weekdays;
}
Simple code for day count:
function dayCount($day,$month,$year){
$totaldays = date('t',strtotime($year.'-'.$month.'-01'));
$countday = 4;
if(($totaldays - $day) >= 28 ){
$countday = 5;
}
return $countday;
}
echo dayCount(1,9,2019);
$day =Carbon\Carbon::now("Asia/Kolkata")->daysInMonth;
echo $day.'</br>';
if($day = 28) {
$weekend = 8;
}elseif($day = 29) {
$first_day_saturday = Carbon\Carbon::now("Asia/Kolkata")->firstOfMonth()->isSaturday();
$first_day_sunday = Carbon\Carbon::now("Asia/Kolkata")->firstOfMonth()->isSunday();
if($first_day_saturday || $first_day_sunday) {
$weekend = 9;
}else {
$weekend = 8;
}
}elseif($day = 30) {
$first_day_saturday = Carbon\Carbon::now("Asia/Kolkata")->firstOfMonth()->isSaturday();
$first_day_sunday = Carbon\Carbon::now("Asia/Kolkata")->firstOfMonth()->isSunday();
if ($first_day_saturday) {
$weekend = 10;
}elseif ($first_day_sunday) {
$weekend = 9;
}else {
$weekend = 8;
}
}else {
$first_day_thrusday = Carbon\Carbon::now("Asia/Kolkata")->firstOfMonth()->isThursday();
$first_day_friday = Carbon\Carbon::now("Asia/Kolkata")->firstOfMonth()->isFriday();
$first_day_saturday = Carbon\Carbon::now("Asia/Kolkata")->firstOfMonth()->isSaturday();
$first_day_sunday = Carbon\Carbon::now("Asia/Kolkata")->firstOfMonth()->isSunday();
if ($first_day_friday || $first_day_saturday) {
$weekend = 10;
}elseif ($first_day_sunday || $first_day_thrusday) {
$weekend = 9;
}else {
$weekend = 8;
}
}
echo $weekend.'</br>';
I need to include a php calendar on the sidebar of my page.
I am using a snippet I found weeks ago, as I have used it before and it works fine. But this time, I have to add next and previous buttons to display the previous or next month...
My question is... do I need to modify the php that generates the current month?
this is what I have so far:
<table class="month">
<tr class="days">
<td>Mon</td>
<td>Tues</td>
<td>Wed</td>
<td>Thurs</td>
<td>Fri</td>
<td>Sat</td>
<td>Sun</td>
</tr>
<?php
$today = date("d"); // Current day
$month = date("m"); // Current month
$year = date("Y"); // Current year
$days = cal_days_in_month(CAL_GREGORIAN,$month,$year); // Days in current month
$lastmonth = date("t", mktime(0,0,0,$month-1,1,$year)); // Days in previous month
$start = date("N", mktime(0,0,0,$month,1,$year)); // Starting day of current month
$finish = date("N", mktime(0,0,0,$month,$days,$year)); // Finishing day of current month
$laststart = $start - 1; // Days of previous month in calander
$counter = 1;
$nextMonthCounter = 1;
if($start > 5){ $rows = 6; }else {$rows = 5; }
for($i = 1; $i <= $rows; $i++){
echo '<tr class="week">';
for($x = 1; $x <= 7; $x++){
if(($counter - $start) < 0){
$date = (($lastmonth - $laststart) + $counter);
$class = 'class="blur"';
}else if(($counter - $start) >= $days){
$date = ($nextMonthCounter);
$nextMonthCounter++;
$class = 'class="blur"';
}else {
$date = ($counter - $start + 1);
if($today == $counter - $start + 1){
$class = 'class="today"';
}
}
echo '<td '.$class.'><span class="dayWrap">'. $date . '</span></td>';
$counter++;
$class = '';
}
echo '</tr>';
}
?>
</table>
<div class="changeMonthLinks">
<a class="col-xs-12" href="">< Prev</a>
<a class="col-xs-12 aright" href="">Next ></a>
</div>
I just don't know how to proceed...or what do I need to add in the anchor tags :S
Any help will be appreciate it.
Thank you
Thank you!!
I've added $now for the param ?now in the url and I'm parsing it with strtotime to the variable $dtNow, all the date functions are extended and the links in the bottom are extended with ?now=$dtNow + 1 month and ?now=$dtNow - month
Here's the code
<?php
$now = '';
if(isset($_GET['now']))
$now = $_GET['now'];
$dtNow = strtotime($now);
if(!$dtNow)
{
$dtNow = time();
}
echo "<h1>Today is " . date('Y-m-d', $dtNow) . "</h1>";
?>
<table class="month">
<tr class="days">
<td>Mon</td>
<td>Tues</td>
<td>Wed</td>
<td>Thurs</td>
<td>Fri</td>
<td>Sat</td>
<td>Sun</td>
</tr>
<?php
$today = date("d", $dtNow); // Current day
$month = date("m", $dtNow); // Current month
$year = date("Y", $dtNow); // Current year
$days = cal_days_in_month(CAL_GREGORIAN,$month,$year); // Days in current month
$lastmonth = date("t", mktime(0,0,0,$month-1,1,$year)); // Days in previous month
$start = date("N", mktime(0,0,0,$month,1,$year)); // Starting day of current month
$finish = date("N", mktime(0,0,0,$month,$days,$year)); // Finishing day of current month
$laststart = $start - 1; // Days of previous month in calander
$counter = 1;
$nextMonthCounter = 1;
if($start > 5){ $rows = 6; }else {$rows = 5; }
for($i = 1; $i <= $rows; $i++){
echo '<tr class="week">';
for($x = 1; $x <= 7; $x++){
if(($counter - $start) < 0){
$date = (($lastmonth - $laststart) + $counter);
$class = 'class="blur"';
}else if(($counter - $start) >= $days){
$date = ($nextMonthCounter);
$nextMonthCounter++;
$class = 'class="blur"';
}else {
$date = ($counter - $start + 1);
if($today == $counter - $start + 1){
$class = 'class="today"';
}
}
echo '<td '.$class.'><span class="dayWrap">'. $date . '</span></td>';
$counter++;
$class = '';
}
echo '</tr>';
}
?>
</table>
<div class="changeMonthLinks">
<a class="col-xs-12" href="?now=<?php echo date('Y-m-d', $dtNow - 30*24*60*60); ?>">< Prev</a>
<a class="col-xs-12 aright" href="?now=<?php echo date('Y-m-d', $dtNow + 30*24*60*60); ?>">Next ></a>
</div>
I am working on a calendar in PHP. With the calendar, I am able to query a mysql database and it will show the number of events for each day. The problem I'm having is that it is showing the start and end dates but i need it to also show all the days inbetween.Thus, showing that the days inbetween are also occupied. My current Table has DepDate (departure) AND RetDate (Return Date). Im thinking this has to be done through the mysql query. Any help would be awesome! Thanks buds
<script>
function goLastMonth(month, year, keyname){
if(month == 1) {
--year;
month = 13;
}
--month
var monthstring= ""+month+"";
var monthlength = monthstring.length;
var keyname = "<?php echo $searchTerm; ?>";
if(monthlength <=1){
monthstring = "0" + monthstring;
}
document.location.href ="<?php $_SERVER['PHP_SELF'];?>?month="+monthstring+"&year="+year+"&keyname="+keyname;
}
function goNextMonth(month, year, keyname){
if(month == 12) {
++year;
month = 0;
}
++month
var monthstring= ""+month+"";
var monthlength = monthstring.length;
var keyname = "<?php echo $searchTerm; ?>";
if(monthlength <=1){
monthstring = "0" + monthstring;
}
document.location.href ="<?php $_SERVER['PHP_SELF'];?>?month="+monthstring+"&year="+year+"&keyname="+keyname;
}
</script>
</head>
<?php
if (isset($_GET['day'])){
$day = $_GET['day'];
} else {
$day = date("j");
}
if(isset($_GET['month'])){
$month = $_GET['month'];
} else {
$month = date("n");
}
if(isset($_GET['year'])){
$year = $_GET['year'];
}else{
$year = date("Y");
}
$currentTimeStamp = strtotime( "$year-$month-01");
$monthName = date("F", $currentTimeStamp);
$numDays = date("t", $currentTimeStamp);
$currentTimeStamp = strtotime( "$day-$month-$year"); // added this to reset this value for prev* and next* code
$counter = 0;
?>
<?php
include ('plane.css');
/* ------------------------------------------------------------Calendar Creation---------------------------------------------------------------------- */
include ('planeh.html');
?>
<?php
//get previous month
$prevMonth = $month - 1;
$prevYear = $year;
if($prevMonth <= 0){
$prevMonth = 12;
$prevYear--;
}
$pMonthStr = "" + $prevMonth;
if(strlen($pMonthStr) <= 1)
$pMonthStr = "0" + $pMonthStr;
//get num of day for previous month
$previousTimeStamp = strtotime( "01-$pMonthStr-$prevYear");
$prevNumDays = date("t", $previousTimeStamp);
$numDays = date("t", $currentTimeStamp);
$counter = 0;
for($i = 1; $i < $numDays+1; $i++, $counter++){
$timeStamp = strtotime("$year-$month-$i");
if($i == 1) {
$firstDay = date("w", $timeStamp);
for($j = 0; $j < $firstDay; $j++, $counter++) {
$prevDay = $prevNumDays-$firstDay+$j+1;
echo "<td class = td3><div id = prev>$prevDay</div></td>";
}
}
if($counter % 7 == 0) {
echo"</tr><big></big><tr>";
}
/* Up to this point, everything is the development of the calendar */
$monthstring = $month;
$monthlength = strlen($monthstring);
$daystring = $i;
$daylength = strlen($i);
if($daylength <=0){
$daystring = "0".$daystring;
}
// links and date located on calendar
$todaysDate = date("m/d/y");
/* Indicates date located on calendar */
echo "<td align = center class = td3><div class = button>".$i."</a></div>";
$sqlEvent2 = mysql_query("select * FROM trips where DepDate = '".$year."-".$month."-".$i."'");
$num_rows = mysql_num_rows($sqlEvent2);
if(mysql_errno()){
echo "MySQL error ".mysql_errno().": "
.mysql_error()."\n<br>When executing <br>\n$query\n<br>";
}
echo '<div id="button">';
echo "<a href='".$_SERVER['PHP_SELF']."?month=".$monthstring."&day=".$i."&year=".$year."&v=true' >".$num_rows."</a></td>";
echo '</div>';
}
echo "<tr>";
echo"</table>";
?>
<div id ="menu">
<?php include ('menu2.php');?>
</ul>
</div>
</div>
</tr>
</body>
</html>
All you need here is to reference the MySQL date and time functions, specifically datediff: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_datediff
An example would be:
SELECT start_dt, end_dt, datediff(end_dt, start_dt) as days_elapsed
FROM my_table
where start_dt and end_dt are both date valued.
I have array of week numbers from 1 to 52. how i can convert it to
[week 1 jan],[week 2 jan] .......
using PHP
OK ... I fix it and here is my code
function getWeeks($date, $rollover)
{
$cut = substr($date, 0, 8);
$daylen = 86400;
$timestamp = strtotime($date);
$first = strtotime($cut . "00");
$elapsed = ($timestamp - $first) / $daylen;
$i = 1;
$weeks = 1;
for($i; $i<=$elapsed; $i++)
{
$dayfind = $cut . (strlen($i) < 2 ? '0' . $i : $i);
$daytimestamp = strtotime($dayfind);
$day = strtolower(date("l", $daytimestamp));
if($day == strtolower($rollover)) $weeks ++;
}
return $weeks;
}
and in the foreach I added
$x="1/1/2013 + ".$record->tms." weeks";
$m=date("Y-m-d", strtotime($x));
$first_week_start=getWeeks($m, "sunday");
if($first_week_start == 1){$typo="st";}
if($first_week_start == 2){$typo="nd";}
if($first_week_start == 3){$typo="rd";}
if($first_week_start == 4){$typo="th";}
if($first_week_start == 5){$typo="th";}
$month=date("M", strtotime($m));
$final_format .= "'".$first_week_start.$typo ." week in ".$month."'";
Try a loop, start with 1/jan, use DateInterval::createFromDateString('1 week'); and DateTime::add each time in the loop to add the next week, use DateTime::format to get the month also checking the current year in each iteration to make sure the loop hasn't moved to the next year.
Code:
date_default_timezone_set("UTC");
$weeks = array();
$dt = new DateTime("2013-01-01");
$interval = DateInterval::createFromDateString("1 week");
for($i=1; $i <= 52; $i++)
{
$weeks[$i] = "week " . $i . " " . $dt->format("M");
$dt = $dt->add($interval);
}
print_r($weeks);
If you want a function that will return a month abbreviation from a week:
function weekMonth($week)
{
date_default_timezone_set("UTC");
return (new DateTime("2013-01-01"))->add(DateInterval::createFromDateString($week." week"))->format("M");
}
echo weekMonth(6);