Please, i need assistance in this code.I have checked others in Stakeoverflow, but it is not combatible, hence this question. I want to generate all working /weekdays between two dates.I have found a code, but it is generating all days, including weekend. How do i eliminate the weekend from the list or ensure the list generated is ONLY for weekdays?
<?php
$start_Date = date('Y-m-d');
$end_Date = date('Y-m-d', strtotime('30 weekdays'));
//echo $start_Date."<br/>";
//echo $end_Date."<br/>";
// Specify the start date. This date can be any English textual format
$date_from = $start_Date;
$date_from = strtotime($date_from); // Convert date to a UNIX timestamp
// Specify the end date. This date can be any English textual format
$date_to = $end_Date;
$date_to = strtotime($date_to); // Convert date to a UNIX timestamp
// Loop from the start date to end date and output all dates inbetween
$c = 0;
for ($i = $date_from; $i <= $date_to; $i += 86400) {
$c++;
echo $c . "=> " . date("Y-m-d", $i) . '<br />';
}
I expect 30days to be generated but with this code, I am getting 42days . Weekend has been added,instead of weekdays ONLY .
Just add this to your loop:
$w = date('w',$i);// day of week - Sunday == 0, Saturday == 6
if($w == 0 || $w == 6){
continue;
}
DEMO
Your code is almost working only have to add a if checking in your code
your code
for ($i = $date_from; $i <= $date_to; $i += 86400) {
$c++;
echo $c . "=> " . date("Y-m-d", $i) . '<br />';
}
please replace with that one
for ($i = $date_from; $i <= $date_to; $i += 86400) {
$day = date("w", $i);
if($day != 0 && $day!= 6){ // will continue if not Sunday or Saturday
$c++;
echo $c . "=> " . date("Y-m-d", $i) . '<br />';
}
}
You also can take help from php.net
Thanks
You may need to get the day of the week, like date("D"), then use it in your for loop to check..something like this?:
$Weekends = array("Sat","Sun");
for....
$DayOfWeek = date("D",$i);
if(!in_array($DayOfWeek, $Weekend)){
// increment...
}
Related
RESOLVED in answer below
Creating a scheduling tool for my users and I am having an issue trying to increment my variable obtained from a dropdown selection:
$date_select = $_POST['date_select'];
I'm sure it's a textbook fix, but to put it simply...I need to increment $date_select by +1 week for 52 weeks.
I have a dropdown menu starting with the current date, and looping to the end of 365 days, incrementing by 1. No problem here.
<select name="date_select" form="create_schedule">
<?php
for($i = 0; $i <= 365; $i++){
$d=strtotime($i . " Day");
$day = date("n-j-y l", $d) . "<br>";
echo "<option>" . $day . "</option>";
}
?>
</select>
This selection is represented by:
$date_select = $_POST['date_select'];
Next to that, before submitting, users can select a radio button - either M, T, W, Th, F, Sat, or Sun to indicate if they would like to apply their request to that selected day, for every week, for the rest of the year. (Which is what I'm trying to do...which is: increment $date_select by "+1 Week" until the for loop is finished)
This selection is represented by:
$repeat = $_POST['repeat'];
This is the closest I've gotten...the code below increments for every "Monday" like I want for example...if $repeat == 'M', but the numerical dates are wrong...
if(isset($_POST['repeat'])){
for($i = 0; $i <= 52; $i++){
$date = strtotime($i . " week", strtotime($date_select));
echo date("n-j-y l", $date) . "<br/>";
}
For example: if the date selected is 7-4-16 Monday, the output is this:
11-26-07 Monday
12-3-07 Monday
12-10-07 Monday
12-17-07 Monday
12-24-07 Monday
12-31-07 Monday
1-7-08 Monday
And so forth...
Thank you in advance.
RESOLVED The issue was in the date format..."m-d-Y" is not equivalent to "m/d/Y" when incrementing days weeks or months in regards to how it is output. Somewhere along the lines, "American" date format values and "European" date format values were getting mixed up. I changed the date format within both of the for-loops and got it working.
"Note: Be aware of dates in the m/d/y or d-m-y formats; if the separator is a slash (/), then the American m/d/y is assumed. If the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed. To avoid potential errors, you should YYYY-MM-DD dates or date_create_from_format() when possible."
http://www.w3schools.com/php/func_date_strtotime.asp
Here is the working solution in case anyone is trying to do something similar
<select name="date_select" form="create_schedule">
<?php
for($i = 0; $i <= 365; $i++){
$d=strtotime($i . " Day");
$day = date("m/d/Y l", $d) . "<br>";
echo "<option>" . $day . "</option>";
}
?>
</select>
if(isset($_POST['repeat'])){
$repeat = $_POST['repeat'];
echo "<br/>";
for($i = 0; $i <= 13; $i++){
$d=strtotime($i . " week", strtotime($date_select));
echo date("m/d/Y l", $d) . "<br/>";
}
}
So on PHP you have to classes that can be really helpfull doing that
\DateTime and \DateInterval
So to do what you want I would recommend
$firstDate = \DateTime::createFromFormat('Y-m-d', $date_select));
$baseDate = clone $firstDate;
$intervalToAdd = new \DateInterval('P1w')
if(isset($_POST['repeat'])){
for($i = 0; $i <= 52; $i++){
$date [$i] = $baseDate->add($intervalToAdd);
echo '<option>'.$date[$i]->format('Y-m-d').'</option>';
}
I am building a PHP Calendar, and I would like to highlight today with the Bootstrap class info. This is my code so far that loops through and displays the days:
<?php
$timestamp = mktime(0, 0, 0, $current_month, 1, $current_year);
$max_day = date("t", $timestamp);
$this_month = getdate($timestamp);
$start_day = $this_month["wday"];
for ($i = 0; $i < ($max_day + $start_day); $i++) {
if ($i % 7 == 0) {
echo "<tr>\n";
}
if ($i < $start_day) {
echo "<td></td>\n";
} else {
echo "<td class=\"text-center\" style=\"height:100px;valign:middle\">" . ($i - $start_day + 1) . "</td>\n";
}
if ($i % 7 == 6) {
echo "</tr>\n";
}
}
?>
I would somehow like to get today's date (with date()?) and compare it to the date currently being used within my for loop, adding a class to the <td> if they match. However, I'm scratching my head trying to figure out exactly how to do this. Any help would be appreciated.
why don't you compare them using strtotime() ? You can turn $max_day into a comparable timestamp like this
$unix_max_day = strtotime($max_day); and compare it to $timestamp
so for example...
if($unix_max_day < $timestamp){
echo '$max_day is less than $timestamp';
}
Hope this is what you meant... :)
I had to do three checks because the date I am using is in the array $this_month[], one checking the day was the same, one checking the month was the same, and then one checking the year was the same.
Putting my result into a variable $td like so:
$td = ((($i - $start_day + 1) == date("j")) && ($this_month["mon"] == date("n")) && ($this_month["year"] == date("Y")) ? "info" : "");
Who knows if this is the most efficient way of doing it.
How do I get the exact dates of the last 7 days including today in a custom format (dd/mm)?
In the resulting array I would like to get something like (dates are examples only):
1=>11/2 (today minus 7 days)
2=>12/2 (today minus 6 days)
...
7=>17/2 (today)
function getLastNDays($days, $format = 'd/m'){
$m = date("m"); $de= date("d"); $y= date("Y");
$dateArray = array();
for($i=0; $i<=$days-1; $i++){
$dateArray[] = '"' . date($format, mktime(0,0,0,$m,($de-$i),$y)) . '"';
}
return array_reverse($dateArray);
}
Usage:
$arr = getLastNDays(7);
or
$arr = getLastNDays(7, 'd/m/Y');
You can combine the 2 functions date() and strtotime(). for example:
echo date("Y-m-d", strtotime("7 days ago"));
Try:
for ($i=0; $i<7; $i++)
{
echo date("d/m", strtotime($i." days ago")).'<br />';
}
You should be able to work out how to get them in the correct order and into an array :)
Hope that helps
time() gives you the current timestamp.
86400 seconds are one day (60 * 60 * 24).
date() gives you a custom date string.
for ($iDay = 6; $iDay >= 0; $iDay--) {
$aDays[7 - $iDay] = date('d/m', time() - $iDay * 86400);
}
Also see this example.
If you don't want the leading zeros, use 'j/n' as custom date format parameter:
for ($iDay = 6; $iDay >= 0; $iDay--) {
$aDays[7 - $iDay] = date('j/n', time() - $iDay * 86400);
}
Also see this updated example.
=== UPDATE ===
#Dagon's idea to use strtotime() to get the timestamp is great. Here the better solution:
for ($iDay = 6; $iDay >= 0; $iDay--) {
$aDays[7 - $iDay] = date('j/n', strtotime("-" . $iDay . " day"));
}
And an example.
I need help Select every other Wednesday starting on 5/2/12. This code below selects every other Wednesday starting on the week it currently is. But i need to set the beginning week. I am familiar with PHP, but not familiar with php dates. So please be as specific as possible.
I found this:
$number_of_dates = 10;
for ($i = 0; $i < $number_of_dates; $i++) {
echo date('m-d-Y', strtotime('Wednesday +' . ($i * 2) . ' weeks')). "<br>".PHP_EOL;
}
Use mktime to create your starting date and pass that as the second argument to strtotime so that counting starts from there:
$startDate = mktime(0, 0, 0, 5, 2, 2012); // May 2, 2012
for ($i = 0; $i < $number_of_dates; $i++) {
$date = strtotime('Wednesday +' . ($i * 2) . ' weeks', $startDate);
echo date('m-d-Y', $date). "<br>".PHP_EOL;
}
See it in action.
Give it a date in the string, instead of "Wednesday" (that chooses the next Wednesday), write:
strtotime('20120502 +' . ($i * 2) . ' weeks'))
To choose that date. (Format is yyyymmdd).
If you have PHP 5.2.0 or newer, you can do it easily this way:
$date = new DateTime('2006-05-02');
for ($i=0; $i<10; $i++) {
echo $date->format('m-d-Y').'<br/>'.PHP_EOL;
$date->modify('+1 week');
}
You could also use the DatePeriod and DateInterval classes to make life easier.
Standard disclaimer: both of the classes above require PHP >= 5.3.0.
$number_of_dates = 10;
$start_date = new DateTime("5/2/12");
$interval = DateInterval::createFromDateString("second wednesday");
$period = new DatePeriod($start_date, $interval, $number_of_dates - 1);
foreach ($period as $date) {
echo $date->format("m-d-Y") . "<br>" . PHP_EOL;
}
I've been reading about problems in php with strtotime and "next month" issues. What i want to make is counter of months between two dates.
For example if I have start date 01.02.2012 and stop date 07.04.2012 I'd like to get return value - 3 months. Also 3 months would be the result if start date i 28.02.2012 and 07.04.2012. I am not counting exact number of days/months, just a number of months I have between two dates. It's not a big deal to make it with some strange date, mktime and strtotime usage, but unfortunatelly start and stop dates might be in two different years so
mktime(0,0,0,date('m')+1,1,date('Y');
isnt going to work (i do not now the year and if it changes between start and stop date. i can calculate it but it is not nice solution). Perfect solution would be to use:
$stat = Array('02.01.2012', '07.04.2012')
$cursor = strtotime($stat[0]);
$stop = strtotime($stat[1]);
$counter = 0;
while ( $cursor < $stop ) {
$cursor = strtotime("first day of next month", $cursor);
echo $cursor . '<br>';
$counter++;
if ( $counter > 100) { break; } // safety break;
}
echo $counter . '<br>';
Unfortunatelly strtotime isnt returning proper values. If I use it is returning empty string.
Any ideas how to get timestamp of the first day of next month?
SOLUTION
$stat = Array('02.01.2012', '01.04.2012');
$start = new DateTime( $stat[0] );
$stop = new DateTime( $stat[1] );
while ( $start->format( 'U') <= $stop->format( 'U' ) ) {
$counter ++;
echo $start->format('d:m:Y') . '<br>';
$start->modify( 'first day of next month' );
}
echo '::' . $counter . '..<br>';
<?php
$stat = Array('02.01.2012', '07.04.2012');
$stop = strtotime($stat[1]);
list($d, $m, $y) = explode('.', $stat[0]);
$count = 0;
while (true) {
$m++;
$cursor = mktime(0, 0, 0, $m, $d, $y);
if ($cursor < $stop) $count ++; else exit;
}
echo $count;
?>
the easy way :D