I have a database of events that are both "static" (on a specific day) and "recurring" (starting on a specific day, but set to be "recurring" either every week or every other week). I understand that there needs to be intervals of 7 and 14, but I don't know how to get to that point to find a date > today and spit it out.
so example;
I want to find the next upcoming recurring events and spit out their relevant dates. Side note: the data I'm stuck working with is in strings (I know, I know) of Ymd so 20150821 would be Aug 21st 2015.
if today was Aug 21 2015 and there's a recurring event for "Every other Friday" starting on Aug 7, it would be +14 which would get you to today, Friday Aug 21.
But say there was one for "Every Wednesday" starting Aug 19, I'd want to get the date for Wednesday, Aug 26 and spit that out.
This would need to work for infinite dates in the future, with the start dates never changing.
So running the script on Jan 1 2016, I'd need to know that the next "Every Wednesday" was Jan 6 2016.
pseudo code:
if(start_date < today_date) {
// start date is in the past
add recurring_inverval to start_date until result >= today_date
echo result
} elseif(start_date > today_date {
// start date is in the future
echo start_date
}
it's the adding until x that I'm lost at. not sure how to do that within an if statement.
also not sure if that's the best way to go about it. I know PHP can also do complicated strings and convert them to a date. like "Next Saturday"
I know you answered this yourself, however another option is to just use the modulus (remainder after devision) of the current date minus the start date to compute your next date. Here is a quick script to do just that :
<?php
function nextDate($start_date,$interval_days,$output_format){
$start = strtotime($start_date);
$end = strtotime(date('Y-m-d'));
$days_ago = ($end - $start) / 24 / 60 / 60;
if($days_ago < 0)return date($output_format,$start);
$remainder_days = $days_ago % $interval_days;
if($remainder_days > 0){
$new_date_string = "+" . ($interval_days - $remainder_days) . " days";
} else {
$new_date_string = "today";
}
return date($output_format,strtotime($new_date_string));
}
echo nextDate('20151210',14,'Ymd') . "<br />";
echo nextDate('20150808',14,'Ymd') . "<br />";
?>
You also don't want to return an early date if the "start date" is in the distant future. Code updated to prevent that.
solved it myself:
$begin = new DateTime($start_date); // start searching on the start date of the event
$end = new DateTime(date('Ymd')); // end on today's date
$end = $end->modify('+1 month'); // extend search to the next month
$interval = new DateInterval('P'. $freq_math .'D'); // intervals of Plus X Days - uses frequency math of 7 or 14 for every or every other
$daterange = new DatePeriod($begin, $interval, $end);
foreach($daterange as $date) {
if($date->format('Ymd') >= date('Ymd')) {
$next_recurring_date = $date->format('Ymd');
break;
}
}
echo $next_recurring_date;
Related
I want to find entries in a month range based on date of a month
like if a user registered on 20th of a month, the script should get entries in a range from last 20th to next 20th of the month.
I.e if the script is running on any day before 20th of April the range should be March 20 - April 20, and if its running on 20th April or after then the range should be April 20 - May 20.
I looked up relative formats but it only lists functions for day names and weeks etc.
Is there any way the relative date format works like last n to next n. where n= 1 to 31.
Can anyone help? Thanks
Based on comment from Cully, here is an implementation (it still feels too messy, maybe there is an easier way to do it). It may explain the question a bit more.
function getFromDate($myDate, $nDate)
{
// sub 1 day till date is $nDate
while(true)
{
if($myDate->format('d')==$nDate)
break;
$myDate->sub(new DateInterval('P1D'));
}
return $myDate;
}
function getToDate($myDate, $nDate)
{
// add 1 day till date is $nDate
while(true)
{
$myDate->add(new DateInterval('P1D'));
if($myDate->format('d')==$nDate)
break;
}
return $myDate;
}
$timestamp = 1602107066; // An example user registration date, 7 October 2021
$nDate = gmdate("d", $timestamp);
$fromDate = getFromDate(new DateTime('now'), $nDate);
$toDate = getToDate(new DateTime('now'), $nDate);
echo $fromDate->format('d M y')."\r\n"; // 7 May 2021 (run on May 22 2021)
echo $toDate->format('d M y'); // 7 June 2021 (run on May 22 2021)
Do you mean something like this? It might not be exactly what you want, but can you use it to create what you want?
<?php
$numberOfDaysIWant = 20;
// March 20th, 2021, but you can use any date you want
$startDate = new DateTimeImmutable('2021-03-20');
$myPastDates = [];
$myFutureDates = [];
$currentDate = $startDate;
for($i = 0; $i < $numberOfDaysIWant; $i++) {
$currentDate = $currentDate->sub('P1D');
$myPastDates []= $currentDate;
}
$currentDate = $startDate;
for($i = 0; $i < $numberOfDaysIWant; $i++) {
$currentDate = $currentDate->add('P1D');
$myFutureDates []= $currentDate;
}
var_dump($myPastDates, $myFutureDates);
It's unclear from your question, but it sounds like maybe you want to get the $numberOfDaysIWant value based on the date of the selected month. If so, you could use this to get it:
<?php
$startDate = new DateTimeImmutable('2021-03-20');
$numberOfDaysIWant = (int) $startDate->format('j'); // "j" gives you the day of the month, without leading zeroes
Spent a while searching and couldn't find any answers. Really simple question I hope.
In my database I have a string column dayOfWeek.
I use this to track what day of week weekly events are scheduled for. Usually I match the dayOfWeek to the current day using PHP and display the correct events.
What I want to do is show a calendar into the future which makes a list of the future dates of the weekly recurring event.
For example, if the event takes place on Tuesday, and Feb 2017 has Tuesdays on the 7th, 14th, 21st, and 28th. I'd like to use PHP to display as such.
I know strtotime() is used to do the opposite, find a day from the date, but can the opposite be done easily?
Sorry if my question is poorly worded or missing information.
USE CASE FOR FEB 2017 TUESDAYS
$n=date('t',strtotime("2017-02-01")); // find no of days in this month
$dates=array();
for ($i=1;$i<=$n;$i++){
$day=date("D",strtotime("2017-02-".$i)); //find weekdays
if($day=="Tue"){
$dates[]="2017-02-".$i;
}
}
print_r($dates);
http://phpfiddle.org/main/code/s8wq-xx44
You could do this:
// The day of the week.
$dayOfEvent = row['day'];
// Say, you wanted the next 5 weeks.
$numberOfWeeks = 5;
$tempDate = date(U);
$counter = 0;
do{
$tempDate = $tempDate + 86,164;
if(date('D', $tempDay) == 'Tue'){
echo date('l jS \of F Y', $tempDay);
$counter++;
}
}while($counter < $numberOfWeeks);
First try to find the date for the day in current week. After that keep adding 7 days multiples to it.
<?php
// $timestamp = now();
for ($i=0; $i < 7; $i++) {
$days = $i." days";
$t = date('Y-m-d', strtotime($days));
$dayOfTheDay = date("D",strtotime($t));
if ( $dayOfTheDay == day from table )
{
$current_date = $t;
break;
}
}
for ($i=0; $i < 200; $i++) {
in this loop add 7 days to current_date
}
?>
CONTEXT: My client, a local movie theater, runs a Sunday Matinee Special every other Sunday starting with the SECOND SUNDAY every year. So for this year the dates are 1/11, 1/18, 2/8, 2/22, .... [The only exception is the SUNDAY after their film festival which runs the the THIRD FULL WEEK OF OCTOBER, but automating this single exception is a "would-be-nice" item, not essential.]
MY SKILL LEVEL: BEGINNER (I need your help!) I believe I need to use a combination of mktime() and date() but I don't know how to put it together.
WHAT I'VE TRIED: I suspect the answer is a combination of what I see on these three posts:
(1) a do-while loop to get a specific day of the week from a date range
(2) there may be a shortcut for referencing the second sunday in the ACCEPTED ANSWER here, but I'm not sure this helps
(3) MOST RELEVANT(?): Get the First Sunday of Every Month
END RESULT: I want to display the [Month] and [Day] of the next Sunday Matinee (so I want to find and display the first item in the array AFTER the current date). The text will appear like this: "Next: [Month] [Day]"
Make sense? Let me know if I've forgotten anything.
If it's not too much to ask, please explain your code so I (and others?) can learn from this; but I'd be more than grateful for "merely" a straight-up solution.
Many thanks.
Debra
UPDATE/PROGRESS: This code will get me the array of Sundays:
$startDate = strtotime("second Sunday of ".date('Y')."");
for ($i=0; $i < 27; $i++){
$sundays = date('F j', ($startDate + (($i*14) * 24 * 3600))) . '<br>';
print $sundays;
}
NEXT TO FIGURE OUT: write a statement to find in the array of Sundays the first date after the current date.
This is a pretty manual, procedural solution, but it should work.
<?php
$SECS_PER_DAY = 86400;
# Find the first Sunday on or after a given timestamp
function firstSundayOnOrAfter($time) {
global $SECS_PER_DAY;
# What day of the week is the given date?
$wday = date('w', $time);
if ($wday == 0) {
# it's already a Sunday
return $time;
}
return $time + (7 - $wday) * $SECS_PER_DAY;
}
# return an array of timestamps representing
# (noon on) the special matinee Sundays for the given year
function specialMatineeSundays($year) {
global $SECS_PER_DAY;
# When's the film festival?
$oct1 = mktime(12,0,0,10,1,$year);
$festivalStart = firstSundayOnOrAfter($oct1);
$festivalSkip = $festivalStart + 7 * $SECS_PER_DAY;
# find the first Sunday of the year
$jan1 = mktime(12,0,0,1,1,$year);
$sunday = firstSundayOnOrAfter($jan1);
# Add a week to start with the second Sunday
$sunday += 7 * $SECS_PER_DAY;
# Build up our result list
$result = [];
# As long as the Sunday under examination is still the same year,
# add it to the list (unless it's the post-festival skip date)
# and then add two weeks
while (date('Y',$sunday) == $year) {
if ($sunday != $festivalSkip) {
$result[] = $sunday;
}
$sunday += 14 * $SECS_PER_DAY;
}
return $result;
}
I am trying to get stripe to set a end_trial date on the next occurrence of whatever day of the month the user chooses. i.e. If today is the 16th and the user chooses the 15th I need the unix timestamp for the 15th of the next month. However if today was the 14th I need the timestamp for tomorrow.
I tried the solution found on this SO question Find the date for next 15th using php .
When i ran the code suggested in that question and substituted 15 for 31
$nextnth = mktime(0, 0, 0, date('n') + (date('j') >= 31), 31);
echo date('Y-m-d', $nextnth);
The result is 2013-03-03
I also tried this one Get the date of the next occurrence of the 18th .
The second one would actually give me 2013-03-31 when i ran it one 2013-1-31.
Both had unexpected results. Is february the problem? Any guidance will be much appreciated.
Here is a way to do it.
function nextDate($userDay){
$today = date('d'); // today
$target = date('Y-m-'.$userDay); // target day
if($today <= $userDay){
$return = strtotime($target);
}
else{
$thisMonth = date('m') + 1;
$thisYear = date('Y');
if($userDay >= 28 && $thisMonth == 2){
$userDay = 28;
}
while(!checkdate($thisMonth,$userDay,$thisYear)){
$thisMonth++;
if($thisMonth == 13){
$thisMonth = 1;
$thisYear++;
}
}
$return = strtotime($thisYear.'-'.$thisMonth.'-'.$userDay);
}
return $return;
}
// usage
echo date('Y-m-d',nextDate(29));
We get the user's choice and compare it today.
If today is less than or equal to user choice, we return the timestamp for this month.
If today is greater than user choice, we loop through dates, adding a month (or a year if it's $thisMonth hits 13). Once this date does exist again, we have our answer.
We check the dates using php's checkdate function, strtotime and date.
I really don't understand the question completely. You can easily determine the date for next 30 days for example
$next_ts = time() + 30 * 86400; // add 30 days to current timestamp
$next = date('Y-m-d', $next_ts); // format string as Y-m-d
echo $next;
If that is not what you need, please explain the problem.
I have used numorous date functions in the past, but I need help on a specific function...
Is there a function where I can assign two unknown dates and the system knows where I am within these two dates? Let me explain:
Our monthly salaries run from the 23rd of each month to the 22nd of the next month. Because earnings work on an hourly basis, my boss wants to know anywhere within the month where the accumulative salaries are. For instance the salary period started on the 23rd of September 2012 and we are now on the 29th, I want my query to be able to know where we are in the current salary period.
As you know, months move on, thus my script must automatically know in which period and where whithin that period we are now.
I can do the queries surrounding this, I just need to know the date function (s) to use to assign this array...
Any help will be appreciated - Thanx
You can use PHP's DateTime classes to do this quite easily:-
$periodStart = new DateTime('23rd September');
$now = new DateTime();
$interval = $now->diff($periodStart);
echo "We are {$interval->d} days into the payment period";
Output:
We are 6 days into the payment period.
I prefer to extend the DateTime class for this kind of thing, so everything is in the same place:-
class MyDateTime extends DateTime
{
public function elapsedDays(DateTime $since = null)
{
if ($since === null) {
$since = new DateTime();
}
$interval = $since->diff($this);
return (int) $interval->d;
}
}
$periodStart = new MyDateTime('23rd September');
echo "We are {$periodStart->elapsedDays()} days into the payment period";
Gives the same output.
You can then create periods and intervals and iterate over it to aggregate the sum like:
$datePeriodStart = new DateTime('23rd September');
$datePeriodEnd = clone $datePeriodStart;
$datePeriodEnd->add(new DateInterval('P1M'));
$dateToday = new DateTime();
$interval1 = $dateToday->diff($datePeriodStart);
$interval2 = $dateToday->diff($datePeriodEnd);
echo "We are {$interval1->d} day(s) into the payment period, {$interval2->d} day(s) left.\n";
$period = new DatePeriod($datePeriodStart, new DateInterval('P1D'), $dateToday);
$days = new IteratorIterator($period);
$totalSalary = 0;
$totalDays = 0;
foreach($days as $day)
{
$salary = get_salary_for_day($day);
$totalSalary += $salary;
$totalDays++;
printf("#%d: %s %s\n", $totalDays, $day->format('Y-m-d'), number_format($salary));
}
printf("Total Salary for %d day(s): %s\n", $totalDays, number_format($totalSalary));
Example output:
We are 6 day(s) into the payment period, 23 day(s) left.
#1: 2012-09-23 12,500
#2: 2012-09-24 12,500
#3: 2012-09-25 12,500
#4: 2012-09-26 12,500
#5: 2012-09-27 12,500
#6: 2012-09-28 12,500
#7: 2012-09-29 12,500
Total Salary for 7 day(s): 87,500
You could simply use a TIMESTAMP value (seconds since epoch, also called a "unix timestamp") and just test to see if the current date in unix timestamp is in between the first and last unix timestamp dates.
Essentially, that way you are merely converting the date into a big integer (number of seconds since 1969/70), and arithmetic and testing functions become a LOT easier to handle.
Get FROM and TO date:
$to = new DateTime();
$from = new DateTime($to->format('Y-m-23'));
if ($to->format('j') < 23) {
$from->modify('-1 month');
}
Var_dump:
var_dump($from->format('Y-m-d')); # 2012-09-23
var_dump($to->format('Y-m-d')); # 2012-09-29
SQL
$sql = "
SELECT ...
FROM ...
WHERE some_time BETWEEN '" . $from->format('Y-m-d') . "' AND '" . $to->format('Y-m-d') ."'
";