I've made the following date selector in PHP for a timetable but when i use it, it starts at the start of PHP time (1970). How do I make it show the current date?
//date selector
$weekStart = 1;
$date = date('Y').'-'. date('m').'-'. date('d');
$timestamp = strtotime($date);
$dayOfWeek = date('N', $timestamp);
echo("<br><br><form action='' name='timetable' method='Post'><div style='position:relative;left:40%;'><h4>Select Week Beginning</h4></div><div style='float:middle;position:relative;left:39%;'><select onchange='submit();' name='date'>");
for ($i = 0; $i <= 10; $i++) {
$startDate = mktime(0,0,0, date('n', $timestamp), date('j', $timestamp) - $dayOfWeek + $weekStart + ( $i * 7), date('Y', $timestamp));
if(date('d-m-Y', $startDate) == $_POST['date']){
$selected = "selected='selected'";
}
else{
$selected = "";
};
echo("<option ".$selected." >". date('d-m-Y', $startDate)."</option>");
}
echo('</select></form></div><br>');
echo("<table style='border:1px solid;'>");
$weekStart = 0;
if(isset($_POST['date'])){
$timestamp = strtotime($_POST['date']);
}else{
$timestamp = strtotime($date);
};
for ($i = 1; $i <= 7; $i++) {
$dayrow = mktime(0,0,0, date('n', $timestamp), date('j', $timestamp) - $dayOfWeek + $weekStart + $i , date('Y', $timestamp));
echo("<td style='text-align:left;width:800px;margin-top:10px;margin-bottom:10px;padding-left:10px;'><h4>".date('D j-m-Y', $dayrow)."</h4></td>");
echo("<td >");
Thanks In Advance
Related
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>';
Consider there are 44 days between two dates.The result what I want is 2 months and not 1 month or 1 month 14 days.I have tried several date functions in both php and mysql,but failed in obtaining the exact result. I've also tried my own(below)code.
$dt1 = some date
$dt2 = some date
$date1 = date_create("".$dtl."");
$date2 = date_create("".$dt2."");
$dateDiff = date_diff($date2, $date1);
$probDays = $dateDiff->days;
$probMon = $dateDiff->m;
$probYear = $dateDiff->y;
$month = $probDays / 30;
$totLeave = $month * 1;
if($month > $probmon)
{
$totLeave = $totLeave + 1;
}
But I failed.The code is about adding vacation days to the client.Any solution in php or mysql would be grateful.Thanks in advance for all the volunteers.
Using PHP 5.3, maybe you could try the following:
<?php
$dt1 = "2014-05-12";
$dt2 = "2014-06-15";
$date1 = new DateTime($dt1);
$date2 = new DateTime($dt2);
$months = $date1->diff($date2)->m;
$days = $date1->diff($date2)->d;
if ($days >= 1) $months++;
echo $months." months!";
?>
Try to check number of days, if not equal to zero, then add 1 to months count and return that value of months.
<?php
$dt_dif= $dt2 - $dt1 ;
$y = $tot_exp / 365;
$d = $tot_exp % 365;
$m = $d / 30;
$year = (int)$y;
$month = (int)$m;
$day= (int)$d;
$total="".$month."month(s) ".$day."day(s)";
?>
John Riedel answer is the correct one!
You need to use $start_date->diff($end_date)->m & $start_date->diff($end_date)->d to find out the month & date differences.. and if the days count is more than 0, then you need to increase the months count as stated here...
http://www.phpguy.in/finding-difference-between-2-dates-in-php/
$date_from = "2011-01-22";
$date_to = "2011-03-23";
$date_from = date('Y-m-d', strtotime($date_from));
$date_to = date('Y-m-d', strtotime($date_to));
$y1 = date('Y', strtotime($date_from));
$y2 = date('Y', strtotime($date_to));
$m1 = date('m', strtotime($date_from));
$m2 = date('m', strtotime($date_to));
$day1 = date('d', strtotime($date_from));
$day2 = date('d', strtotime($date_to));
$yearDiff = $y2 - $y1;
if ($m2 > $m1) {
$month = $m2 - $m1;
} else {
$month = 0;
}
if ($yearDiff > 0 && $m1 > $m2) {
$yearMonth = (($yearDiff * 12) - ($m1 - $m2));
} else {
$yearMonth = $yearDiff * 12;
}
if ($day1 > $day2) {
$month = ($month - 1);
}
$total_month = $yearMonth + $month;
$total_month = ($total_month > 1) ? $total_month . " months" : $total_month . " month";
echo "Total " . $total_month;
I'm generating dates using my code I want to exclude sunday and saturday please check my code here
for ($date = $start_date; $date <= $end_date; $date = date('Y-m-d', strtotime($date . ' + 1 day'))) {
$week = date('W', strtotime($date));
$year = date('Y', strtotime($date));
$from = date("Y-m-d", strtotime("$date"));
if ($from < $start_date)
$from = $start_date;
$to = date("Y-m-d", strtotime("$date-1day + 1 week"));
if ($to > $end_date) {
$to = $end_date;
}
if ($from <= $to) {
array_push($weekfrom, $from);
array_push($weekto, $to);
}
$n = count($weekfrom);
for ($i = 0; $i < $n; $i++) {
echo $weekfrom[$i];
}}
you can do like this.
$getDate = date('l', strtotime($date));
if ($getDate != 'Saturday' AND $getDate != 'Sunday') {
......
}
if that date not Saturday or Sunday, then process the thing.
Just add this to beginning of your loop:
if(date("w", strtotime($date)) == 0 || date("w", strtotime($date)) == 6) continue;
Like this:
for ($date = $start_date; $date <= $end_date; $date = date('Y-m-d', strtotime($date . ' + 1 day'))) {
if(date("w", strtotime($date)) == 0 || date("w", strtotime($date)) == 6) continue;
$week = date('W', strtotime($date));
$year = date('Y', strtotime($date));
$from = date("Y-m-d", strtotime("$date")); //Returns the date of monday in week
if ($from < $start_date)
$from = $start_date;
$to = date("Y-m-d", strtotime("$date-1day + 1 week")); //Returns the date of sunday in week
if ($to > $end_date) {
$to = $end_date;
}
if ($from <= $to) {
array_push($weekfrom, $from);
array_push($weekto, $to);
}
$n = count($weekfrom);
for ($i = 0; $i < $n; $i++) {
echo $weekfrom[$i];
}}
I used:
strtotime(sprintf('+%d weekday', 3));
if now is 15 sep(Wednesday), the example returns: 20 sep(exclude weekends)
$start_date = '2012-01-01';
$end_date = '2012-12-31';
$total_days = round(abs(strtotime($end_date) - strtotime($start_date)) / 86400, 0) + 1;
if ($end_date >= $start_date)
{
for ($day = 0; $day < $total_days; $day++)
{
echo "<br />" . date("Y-m-d", strtotime("{$start_date} + {day} days"));
}
}
Right now that's printing '1969-12-31' over and over. The expected output should be:
2012-01-01
2012-01-02
2012-01-03
...
2012-12-30
2012-12-31
I would use the DatePeriod class (along with DateTime and DateInterval) for this:
$start_date = '2012-01-01';
$end_date = '2012-12-31';
$start = new DateTime($start_date);
$end = new DateTime($end_date);
$interval = new DateInterval('P1D'); // 1 day interval
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $day) {
// Do stuff with each $day...
echo $day->format('Y-m-d'), "\n";
}
You above code will work with one extra character - you missed a $ on this line:
echo "<br />" . date("Y-m-d", strtotime("{$start_date} + {$day} days"));
// ^ This was missing
<?php
$start_date = '2012-01-01';
$end_date = '2012-12-31';
$total_days = round(abs(strtotime($end_date) - strtotime($start_date)) / 86400, 0) + 1;
if ($end_date >= $start_date)
{
for ($day = 0; $day < $total_days; $day++)
{
echo "<br />" . date("Y-m-d", strtotime("{$start_date} + {$day} days"));
// You missed the $ here ^
}
}
Provided the code stated below, the output are (see below). My question is why after 2009/11/01 it follow's by 2009/11/30 and 2009/12/30 instead of 2009/12/01. From 2009/06/01 ~ 2009/11/01 there is no problem.
output
2009/06/01
2009/07/01
2009/08/01
2009/09/01
2009/10/01
2009/11/01
2009/11/30
2009/12/30
my code
<?php
$startdate = "2009/06/01";
$enddate = "2009/12/31";
$start = strtotime($startdate);
$end = strtotime($enddate);
$currentdate = $start;
while($currentdate < $end)
{
$cur_date = date('Y/m/d',$currentdate);
$month = date('m', $currentdate);
$year = date('Y', $currentdate);
$monthLength = daysOfMonth($month, $year);
$currentdate += $monthLength;
echo $cur_date . "<br />";
}
function daysOfMonth($month, $year)
{
return (86400 * date("t", strtotime($year."-".$month."-01")));
}
?>
<?php
$startdate = "2009/06/01";
$enddate = "2009/12/31";
$start = strtotime($startdate);
$end = strtotime($enddate);
$currentdate = $start;
while($currentdate < $end)
{
$cur_date = date('Y/m/d', $currentdate);
$currentdate = strtotime('+1 month', $currentdate);
echo $cur_date . "<br />";
}
?>