I want to create a list of weekday dates with name of the weekday.
If today is monday - 2017-01-02 (Y-m-d), then I want the list to be something like this:
Mon-02 | Tue-03 | Wed-04 | Thu-05 | Fri-06 | Sat-07 | Sun-08
My code below will give such result, if today is monday.
This is what I have so far.
function NextDayDate($day) {
return new DateTime('next ' . $day);
}
$dt = new DateTime();
$today=date("l");
if ($today=="Monday") {
echo $dt->format('d'); // Todays date
echo '</br>';
}
elseif ($today!="Monday"){
echo '</br>';
$nextMonday = NextDayDate('Monday');
echo $nextMonday->format('d'); // Next date
echo '</br>';
}
if ($today=="Tuesday") {
echo $dt->format('d'); // Todays date
echo '</br>';
}
elseif ($today!="Tuesday"){
echo '</br>';
$nextMonday = NextDayDate('Tuesday');
echo $nextMonday->format('d'); // Next date
echo '</br>';
}
If today is Tuesday, then for monday I will have Mon-09 because that is the next date for monday.
Mon-09 | Tue-03 | Wed-04 | Thu-05 | Fri-06 | Sat-07 | Sun-08
I want to keep last days date so the list does not change.
Maybe there is a way to get current weeks day names with date?
It is much more easy:
$a = new DateTime();
$oneDay = new DateInterval("P1D");
// Here you add one day to your date until it will Monday
while ($a->format('D') != 'Mon') {
$a->add($oneDay);
}
// Here you print 7 days from your target Monday
for ($i = 0; $i < 7; $i++) {
echo $a->format("D-d");
$a->add($oneDay);
}
Related
I'm struggling to formulate this question correctly.
I have a php time() entry in a mysql table column called renew. I have a second entry in the same row under the column type which decides when to update the row, namely: daily or weekly or monthly from the renew column value (date).
How this script works:
In this example on row id 1; the balance must be updated to reflect the budget monthly. The renew date must also be updated to reflect the next monthly incremented date after the update has taken place.
in row id 2; This will take place weekly. and so on.
for example:
| id | renew | type | budget | balance |
|----| ---------- | ---------| ------ | ------- |
| 1 | 1611214417 | monthly | 10,000.00 | 3,000.00 |
| 2 | 1643614417 | weekly | 4,000.00 | 600,00 |
id 1 = 2021-01-21 07:33:37;
id 2 = 2022-01-31 07:33:37;
in row id 1 how to get the next update date from today?
//the time entry
$now = time():
$today = 28/02/2022
$updatetime = $table['renew'];
$updatetype = $table['type'];
How i did the weekly:
from the $updatetime variable above, I incremented the variable with one week until it just passes the $now time.
// what I've done for weekly and seems to be working
for($i = 0; $i < 1000000000; $i++ ){ //random high number for incrementing
if($updatetime < $now){
$updatetime = $updatetime + (86400 * 7); //add one week
} else {
return;
}
}
// the next update will now be the new $updatetime for weekly
How can i do this for the monthly? there are no fixed days in a month, the renew date could fall on the 31 of the month or 29 of February and then the current month could only have 30 days or 28 days.
This is where my issue lies.
If you can assist me.
I would recommend to use PHP DateTime object that is much simpler to manipulate the date time using relative formats
<?php
$today = new DateTime();
$updatetime = '19/01/2021'; // $table['renew']; //19/01/2021
$updatetype = 'weekly'; //$table['type']; //daily || weekly || monthly
$updatetime = (new DateTime())->createFromFormat('d/m/Y', updatetime);
// what I've done for weekly and seems to be working
if($updatetime < $today){
// $updatetime = $today; // reset to today if needed
$updatetime->modify('next week'); // add one week
} else {
return;
}
echo 'Next time of weekly update '. $updatetime->format('Y-m-d') . PHP_EOL . PHP_EOL;
// Output: Next time of weekly update 2022-03-07
// without time reset: Next time of weekly update 2021-01-25
Other examples of date manipulation using relative formats:
<?php
$today = '2022-02-28';
$t = new DateTime($today);
// print the source date time
echo $t->format('Y-m-d') . PHP_EOL;
// examples of continuously manipulate the date time origin
echo $t->modify('next week')->format('Y-m-d') . PHP_EOL;
echo $t->modify('next month')->format('Y-m-d') . PHP_EOL;
echo $t->modify('next year')->format('Y-m-d') . PHP_EOL;
echo $t->modify('next sunday')->format('Y-m-d') . PHP_EOL;
/*
Output:
2022-02-28
2022-03-07
2022-04-07
2023-04-07
2023-04-09
*/
Live demo Live demo without time reset
TIP:
Convert your daily, weekly, monthly into infinitive day, week, month so you will be able to use its value directly using $table['type'] into the date time manipulation:
<?php
$updatetype = 'week'; //$table['type']; //day || week || month
$updatetime->modify('next ' . $table['type']); // add one xxx dynamically!
TIP2*
Use ISO date format Y-m-d to simplify your codding.
$updatetime = '2021-01-19'; //'19/01/2021'
$updatetime = new DateTime($updatetime); // That's it!
Thanks to Ino's answers i worked it out like this:
$today = time(); // can use DateTime() too.
$updatetime = '1611214417'; // $table['renew']; //19/01/2021
$updatetype = 'weekly'; //$table['type']; //daily || weekly || monthly
echo $updatetime.'<br>';
for($i = 0; $i < 100000; $i++){
if($updatetime < $today){
$updatetime = strtotime("+1 week", $updatetime); // can add week month day
} else {
break;
}
}
echo $updatetime.'<br>'; // just for comparison
echo date('Y-m-d', $updatetime); // 2022-03-03 works out perfectly
I have look at 100 calendar code and thkink there is no good one. So i came up with my own code, but need som help to understans what i am missing. Thinking now that i mixed up the loops or somthing have try this for a while but cant get it right. The result i getting now is in a line from top to bottom. The dont seem to work. Can somone help me understand what i need to do?
<table border="1">
<?php
//counter array
$counter = 0;
//start days array
$list=array();
//current day
$curDay = date("28", $today);
// mktime(0, 0, 0, date("m"), date("d") , date("Y"));
$today = mktime(0, 0, 0, date("m"), '28', date("Y"));
// curent month
$curentmonth = '09';
// curent year
$year = 2018;
// curent month
$month = date('m');
//loop number of days in month and list them in the array
for($d=1; $d<=31; $d++)
{
$datetime=mktime(12, 0, 0, $curentmonth, $d, $year);
if (date('m', $datetime)==$curentmonth)
$list[]=date('d', $datetime);
//try to get the right 7 weeks in a month start monday
for ($day = 1; $day <=7; $day++) {
$datetime = strtotime($year.'W'.$month.$day);
echo "<tr>";
}
// make this day red
if ($curentmonth === $month && $list[$counter] === $curDay) {
echo "<td bgcolor='ff0000'>$list[$counter]</td>";
$counter++;
// all other days in the month
} elseif ($month === $curentmonth) {
echo "<td>$list[$counter]</td>";
$counter++;
// other month
} else {
echo "<td> </td>";
echo "</tr>";
}
}
?>
I've tried coercing your code into doing what you want, but I'm afraid there are so many logical problems that it's better to just go back to the drawing board entirely.
Looking at your code, it seems what you want to accomplish is a table that displays the weeks of the current month below each other, with each week starting on monday. For table cells that don't contain a date (e.g. the month starts or ends in the middle of a week) you want to display only a space in the cell so that it is empty.
Additionally, you want to highlight the current date.
So essentially, your intended end result is something like this for today (September 30th, 2018):
+----+----+----+----+----+----+------+
| | | | | | 1 | 2 |
+----+----+----+----+----+----+------+
| 3 | 4 | 5 | 6 | 7 | 8 | 9 |
+----+----+----+----+----+----+------+
| 10 | 11 | 12 | 13 | 14 | 15 | 16 |
+----+----+----+----+----+----+------+
| 17 | 18 | 19 | 20 | 21 | 22 | 23 |
+----+----+----+----+----+----+------+
| 24 | 25 | 26 | 27 | 28 | 29 | *30* |
+----+----+----+----+----+----+------+
There are many ways to accomplish this goal. Here's one way of doing it with simple date manipulation, so that there's no need for arrays and multiple loops:
// the month to render and day to highlight
$today = new DateTime();
// we'll need these to check if we're at the day we need to highlight
// or if we're rendering a date that's outside $today's month
$currentDay = $today->format('j');
$currentMonth = $today->format('n');
$daysInCurrentMonth = $today->format('t');
// figure out what Monday needs to kick off our table
$date = (clone $today)->modify('first day of this month');
if ($date->format('w') != 1) {
$date->modify('previous monday');
}
$shouldStopRendering = false;
echo '<table border="1">';
while (!$shouldStopRendering) {
$weekDay = $date->format('w');
$month = $date->format('n');
$day = $date->format('j');
// start a new table row every time we hit a Monday, note that
// since we forced $date to be a Monday above, our table should
// now always start with a <tr>
if ($weekDay == 1) {
echo '<tr>';
}
if ($month != $currentMonth) {
// we're either at the beginning or end of our table
echo '<td> </td>';
} else if ($day == $currentDay) {
// highlight the current date
echo '<td bgcolor="#ff0000">' . $day . '</td>';
} else {
echo '<td>' . $day . '</td>';
}
if ($weekDay == 0) {
// every time we hit a Sunday, close the current table row
echo '</tr>';
// on a Sunday, if we've gone outside the current month **or**
// this Sunday happens to be the last day we need to render,
// stop looping so we can close the table
if ($month != $currentMonth || $day == $daysInCurrentMonth) {
$shouldStopRendering = true;
}
}
// move on to the next day we need to display
$date->modify('+1 day');
}
echo '</table>';
Note this line:
$date = (clone $today)->modify('first day of this month');
The reason I'm cloning $today instead of just creating a new DateTime instance, is that you may wish to change $today to render a different month. Or maybe you're looping over a number of months to render a calendar for a whole year. I don't know which is the case, so I'm basing $date off of whatever $today happens to be.
Don't just do this:
$date = $today->modify('first day of this month');
That'll work, but it will also modify $today which may not be what you want if you want to re-use the current date after rendering the table.
If you're using an older version of PHP, you may not be allowed to do (clone $today)->modify in a single line. If that's the case, just split it up into two lines:
$date = clone $today;
$date->modify('first day of this month');
Here's a demo of this code in action (with some added whitespace for readability of the generated HTML): https://3v4l.org/Z89i8
I have a database called Holidays which has a date field to save holiday date called STARTTIME, and a duration field to save how many days is the holiday called DURATION. I want to collect all data from that table between some month and year, and print the date 1 by 1.
For Example, here's my data at database:
STARTTIME | DURATION
2016-12-09 | 1
2016-12-15 | 5
How to print it in php 1 by 1 per date? So it will result:
2016-12-09
2016-12-15
2016-12-16
2016-12-17
2016-12-18
2016-12-19
2016-12-20
My code now is like this:
//startDate and endDate is get from my filter, this is only for example
$startDate = '2016-01-01';
$endDate = '2016-01-31';
$sqlHol = "SELECT STARTTIME, DURATION FROM HOLIDAYS WHERE DATEVALUE(STARTTIME) BETWEEN DateAdd('m',-1,#$startDate#) AND #$endDate#";
$queryHol = $db->query($sqlHol);
$hols = $queryHol->fetchAll();
echo '<br />';
//testing to print out array result
print_r($hols);
//looping through data from database
foreach($hols as $hol)
{
for($i=1; $i <= $hol[1]; $i++)
{
echo '<br />';
$due = date_create($hol[0]);
echo date_format($due, 'Y-m-d');
$due = date_add(date_create($hol[0]), date_interval_create_from_date_string('1 days'));
echo '<br />';
}
}
And here's my result (the date doesn't increment):
2015-12-09
2015-12-15
2015-12-15
2015-12-15
2015-12-15
2015-12-15
2015-12-15
Anyone know how to create an output like I need?
It looks like you are writing the due date before you increment it. Try using the loop counter as a way to keep track of how many times you have incremented the date.
foreach($hols as $hol)
{
for($i=0; $i <= $hol[1]-1; $i++)
{
echo '<br />';
$due = date_create($hol[0]);
$due = date_add(date_create($hol[0]), date_interval_create_from_date_string(i.' days'));
echo date_format($due, 'Y-m-d');
echo '<br />';
}
}
What I need is get listed next month days, every new day on new line.
Actually I want it look next:
date | username1 | username2
_________|___________|__________
| |
Mon - 01 | user5 | user2
| |
Tue - 02 | user3 | user2
....
and so on
Later i will be collect these dates and usernames to and update sql table (same type, like example here).
I have this code:
$workdays = array();
$type = CAL_GREGORIAN;
$month = date('n'); // Month ID, 1 through to 12.
$year = date('Y'); // Year in 4 digit 2009 format.
$day_count = cal_days_in_month($type, $month, $year); // Get the amount of days
//loop through all days
for ($i = 1; $i <= $day_count; $i++) {
$date = $year.'-'.$month.'-'.$i; //format date
$workdays[] = $i;
and with foreach echo i get days 1, 2, 3 ... and so on correctly, everyone on new line. But if i place inside foreach this:
<?php foreach($workdays as $value['date']): ?>
<tr>
<td><?php if ($value['date'] != 'dd'){
setlocale(LC_TIME, 'fi_FI.UTF-8');
echo ucfirst(strftime("%a - %d", strtotime($value['date'])));
} else {
echo 'wrong';
}?></td>
<td> </td>
</tr>
<?php endforeach ?>
I get on every new line Mon - 01.
What is wrong (after that, that i newbie on php)?
Try something like this.
<?php
$day_count = date('t',strtotime('+1 month'));
$month = date('m');
$year = date('Y');
if ($month == 12) { $month = 1; $year++; }
for($i = 1; $i<=$day_count; $i++) {
echo '<tr>';
echo '<td>'.date('D',strtotime("$month/$i/$year")).' '.$i."</td>";
echo '<td> </td>';
echo '</tr>';
}
I have a database filled with a bunch of jobs, where I have startDate and endDate.
What Im trying to do is to list current week and 20 weeks ahead, and see if those weeks are within the database.
And when I echo it out I want the date to be green if the date exists and black if not.
It works, but the problem is that it skips the first week. So if I have 2014-07-01 as startDate and 2014-07-14 as endDate, then the second week in that example will get green, not the first one.
This is what I'v done:
UPDATED
$last = 19;
$today = new \DateTime();
$today->modify('Monday this week');
for ($i=0; $i<$last; $i++) {
$the_week = $today->format('o-m-d');
$sql = mysql_query("SELECT startDate,endDate FROM work WHERE '$the_week' BETWEEN startDate AND endDate'");
$row = mysql_fetch_array($sql);
$startdate = $row['startDate'];
$enddate = $row['endDate'];
if(($startdate > $the_week) AND ($enddate < $the_week)) {
$color = "#69dd54;"; } else { $color = "#ffffff;"; }
echo "<span style='color: ". $color ."'>". $the_week ."</span>";
$new_today->modify('next Monday');
}
What is $the_week? It appears to be a date, but there are many dates in a week. For example there is the first date of a week and the last date of the week. It could be that you have a problematic boundary condition here.
You want the start date to be before the last day of a week, and the end date to be after the first day of a week, I am assuming. Therefore the first week gets excluded because its first day is before the start date, and yet its last day is after the start date.
UPDATE:
Consider having:
$the_week = date('o-m-d', strtotime($today. ' + '. $week .' week'));
$the_week_last = date('o-m-d', strtotime($today. ($week +1) .' week'));
By the way, I would recommend you do a single SQL query for this, rather than one for every week. There is an example of how to do that here.
this is the output of your foreach loop for $the_week
2014-07-09
2014-07-16
2014-07-23
2014-07-30
2014-08-06
2014-08-13
2014-08-20
2014-08-27
2014-09-03
2014-09-10
2014-09-17
2014-09-24
2014-10-01
2014-10-08
2014-10-15
2014-10-22
2014-10-29
2014-11-05
2014-11-12
2014-11-19
as you can see the second one is outside of your date range
Try this as your loop
//$first = 0;
$last = 19;
//$weeks = range($first, $last);
$today = new \DateTime();
$today->modify('Monday this week');
for ($i=0; $i<$last; $i++) {
$the_week = $today->format('o-m-d');
$sql = mysql_query("SELECT startDate,endDate FROM work WHERE $the_week BETWEEN startData AND endDate"); //what happend to your original query.
$r = mysql_num_rows($sql);
$row = mysql_fetch_array($sql);
$startdate = $row['startDate'];
$enddate = $row['endDate'];
if(($startdate > $the_week) AND ($enddate < $the_week)) {
$color = "#69dd54;"; } else { $color = "#ffffff;";
}
echo "<span style='color: ". $color ."'>". $the_week ."</span>";
$today->modify('next Monday');
}
outputs
2014-07-07
2014-07-14
2014-07-21
2014-07-28
2014-08-04
2014-08-11
2014-08-18
2014-08-25
2014-09-01
2014-09-08
2014-09-15
2014-09-22
2014-09-29
2014-10-06
2014-10-13
2014-10-20
2014-10-27
2014-11-03
2014-11-10
Using DateTime you can increment by the 'next Monday' or any day for that matter, with a simple for loop.
also make sure to change
$startdate = $ro['startDate'];
$enddate = $ro['endDate'];
to
$startdate = $row['startDate'];
$enddate = $row['endDate'];