php first monday after certain date as string - php

I have a date given as a string. And I would like to get the first monday after this given date, again as a string.
$myDate = "2014-08-24"; // date given in Y-m-d
$nextMonday = ??? // how to get the next monday after $myDate?
This sounds like a question that was solved before, like PHP get next occurrence of Monday from a certain date (with time) or When a date is given how to get the date of Monday of that week in php.
But it actually is a different problem, because I am running into a lot of conversion issues and a little bit confused how to solve this.

you can use this
<?php
$myDate = "2014-08-27";
$next_monday = date('Y-m-d', strtotime("next monday", strtotime($myDate)));
echo $next_monday;
?>
OUTPUT :
2014-09-01
Demo

You can do it using the DateTime and DateTime::modify
$date = new DateTime('2014-08-24');
$date->modify('next monday');
echo $date->format('Y-m-d') . "\n";

Related

PHP dateTime previous month of 31 Dec is wrong

I have this:
$previousMonth = new DateTime('2019-12-31');
$previousMonth->modify('-1 month');
My understanding is '-1 month' should modify the object regardless of number of days in that month, or?
Naturally what should I get or expect to get is end of Nov(2019-11-30) but what I get is first of December(the same month).
BTW if I change the date to '2019-12-30'(one day prior) then it will be end of Nov.
If my initial assumption is not correct, then what is the best alternative to reliably calculate the previous month?
Any thoughts?
The simplest and easiest way to get the last month in php is
$previousMonth = date("Y-n-j", strtotime("last day of previous month"));
Same as been suggested on other thread Getting last month's date in php
$date = "2019-12-31 00:00:00";
echo date('Y-m-d', strtotime($date . '-1month'));
This prints out 2019-12-01 as the 31/11 does not exist.
The following doesn't answer your question but may help in the future. I like to use Carbon when working with dates. Your issue could be resolved quite simply with this.
https://carbon.nesbot.com/
It has many functions and is extremely simple to use, and it can be installed with Composer.
To get the last day of the previous month, you can get the first day of the current month and substract 1 second or 1 day :
$previousMonth = new DateTime('2019-12-31');
$previousMonth->modify($previousMonth->format('Y-m-01')); // date is 2019-12-01 00:00:00
$previousMonth->modify('-1 sec');
echo $previousMonth->format('Y-m-d H:i:s') . PHP_EOL; // Outputs 2019-11-30 23:59:59
$previousMonth->modify('+1 sec'); // set back the original date 2019-12-01 00:00:00
$previousMonth->modify('-1 day');
echo $previousMonth->format('Y-m-d H:i:s'); // Outputs 2019-11-30 00:00:00

How to say "next month 15th" in PHP's DateTime?

What I want to do:
Getting a certain day from a certain month directly via the DateTime methods (no mktime stunts :)), like
$day = new DateTime('15th of next month');
but it's not possible to set a fixed day by it's number.
Can anybody help ?
EDIT: I've changed the DateTime from this month to next month to make the problem more clear.
You can use DateTime::createFromFormat(). If you only pass the day value, it'll default to the current month and year.
$date = DateTime::createFromFormat('d', '15');
echo $date->format('Y-m-d'); // 2018-10-15
Edit for the new question requirements:
$date = DateTime::createFromFormat('d', 15)->add(new DateInterval('P1M'));
echo $date->format('Y-m-d'); // 2018-11-15

Date from database returns in a different form

I'm trying to get today's date, and compare it to the date in my database, but the date in my database returns in a different form from the date that I get from the date function, so if I compare them in an if statement, the values are always going to be false. Is there a way for me to compare them so that it returns as true?
$date = date('y-m-d'); //date from date function ---> 15/07/19
$dateFromDatabase; //date from database ---> 2015/07/19
if ($date == $dateFromDatabase) {
echo "It's the same day.";
}
strtotime
You have in php some great functions to convert your human readable dates to timestamps.
The first magic function is called strtotime (string to time) : give it your date, you get a UNIX timestamp! Let's see some examples:
echo strtotime('2008-04-12 13:24');
echo strtotime('12 april 2008 13:24');
echo strtotime('12.04.2008 13:24');
And more powerfull, strtotime can recognize some keywords:
echo strtotime('now');
echo strtotime('+4 days');
echo strtotime('+1 month');
echo strtotime('next monday');
echo strtotime('+2 weeks 3 days 4 hours 23 minutes');
The second argument of strtotime is a timestamp, and its default value is the actual timestamp (time()). So echo strtotime('+4 days') is relative to the current time. Of course you can also give strtotime your mysql date! (Note you can also use the mysql function UNIX_TIMESTAMP, which use a bit more ressources).
To compare dates, it's now just a detail:
// your first date coming from a mysql database (date fields)
$dateA = '2008-03-01 13:34';
// your second date coming from a mysql database (date fields)
$dateB = '2007-04-14 15:23';
if(strtotime($dateA) > strtotime($dateB)){
// bla bla
}
Better than substring, isn't it ?!
Here is just another example, not relative to current date but to a particular date:
strtotime('23 hours ago', strtotime('2005-04-13 14:00'));
This mean 23 hours ago relatively to the second given date, which must be a timestamp.
user manual doesn't give a complete description of the supported date formats. Strtotime('dd/mm/YYYY') doesn't work, it works only with mm/dd/YYYY format.
date in dd/mm/YYYY format, can be convert it to YYYY-mm-dd by using explode() function, but I think there are better solutions.
$date = '25/05/2010';
$date = str_replace('/', '-', $date);
echo date('Y-m-d', strtotime($date));

php add x weeks to a date and then find the next given day

I can add x week to my date
//$ultima_azione <--- 2015/07/15
//$data['intervallo'] <---- 5
$mydate = date("Y-m-d",strtotime($ultima_azione." +".$data['intervallo']." weeks"));
now how can i give a day starting from that week
example:
//$mydate + "next Monday" -----> final date
and this ve to work like, if today is Monday and i add weeks to jump to an other Monday and then i select the next Monday the week don't ve to change
The simplest way would be to use strtotime. It can do date calculations based on a textual representation of the delta:
$mydate = strtotime('+3 weeks');
It also accepts a second parameter, which is a timestamp to start from when doing the calculation, so after you get the offset in weeks, you can pass the new date to a second calculation:
// Get three weeks from 'now' (no explicit time given)
$mydate = strtotime('+3 weeks');
// Get the Monday after that.
$mydate = strtotime('next Monday', $mydate);
See strtotime documentation for more examples of notations that you can use.
I would highly recommend using PHP's built-in DateTime class for any date and time logic. It's a much better API than the older date and time functions and creates much cleaner and easier to read code.
For example:
// Current date and number of weeks to add
$date = '2015/07/15';
$weeks = 3;
// Create and modify the date.
$dateTime = DateTime::createFromFormat('Y/m/d', $date);
$dateTime->add(DateInterval::createFromDateString($weeks . ' weeks'));
$dateTime->modify('next monday');
// Output the new date.
echo $dateTime->format('Y-m-d');
References:
DateTime.
DateTime::createFromFormat
DateTime::add
DateTime::modify
DateInterval::createFromDateString
DateTime::format
Are you looking for something like this?
$today = time();
$weeks = 2;
// timestamp 2 weeks from now
$futureWeeks = strtotime("+ ".$weeks." weeks");
// the next monday after the timestamp date
$futureMonday = strtotime("next monday",$futureWeeks);
echo date("Y-m-d", $futureMonday);
// or in one line
echo date("Y-m-d", strtotime("next monday", strtotime("+ ".$weeks." weeks")));
PHP is using an unix timestamp for date calculations. Functions as date() and strtotime() using a timestamp as an optional second parameter. This is used a reference for formatting and calculations. If no timestamp is passed to the function the current timestamp is used (time()).
I have the answer here. This will show the next wednesday every 2 weeks and the first date to start from would be the 10th.
I have also added in an estimated delivery which would be 6 weeks after that date.
We will be placing our next order for this on:
<?php
$date = '2020/05/26';
$weeks = 2;
$dateTime = DateTime::createFromFormat('Y/m/d', $date);
$dateTime->add(DateInterval::createFromDateString($weeks . ' weeks'));
$dateTime->modify('wednesday');
echo $dateTime->format('d/m/Y');
?>
Expected delivery for the next order will be:
<?php
$date = '2020/05/26';
$weeks = 2;
$dateTime = DateTime::createFromFormat('Y/m/d', $date);
$dateTime->add(DateInterval::createFromDateString($weeks . ' weeks'));
$dateTime->modify('+42 days next wednesday');
echo $dateTime->format('d/m/Y');
?>
If anyone can confirm this is correct that would be great.

Can strtotime return the date and time of a day AFTER a given date?

I am working on generating recurring dates using PHP to process the dates. I have a $startDateTime and $endDateTime. These two variables are for the first date in the series.
If the even repeats every Tuesday I need something along the lines of
$repeatDay = "Tuesday";
$followingDay = strtotime($startDateTime. " following $repeatDay");
Using "next $repeatDay" doesn't work since I am not generating the date from todays date.
EDIT:
It seems that every five loops the time jumps forward an hour in the date. This may be because $start="2014-04-29 11:00:00" and strtotime is only converting the date correctly.
How should I convert 2014-04-29 11:00:00 to a format that strtotime understands?
$firstOccurrence=strtotime($start);
$nextOccurence=strtotime("next $day", $firstOccurrence); //Set the first recurring date for this day
while($nextOccurence<=strtotime($activeUntil)){
echo date("M d, Y H:m:i",$nextOccurence)." | ";
$nextOccurence=strtotime("next $day", $nextOccurence);
}
Maybe it's time to start working with DateTime? It's pretty complex in modyfing dates. For example, creating date time from your $start would look like this:
$start="2014-04-29 11:00:00";
$dateTime=DateTime::createFromFormat("Y-m-d H:m:i", $start);
And as you have $dateTime, you can modify it by one day:
$dateTime->modify('+1 day');
//or just
$dateTime->modify('next tuesday');
//and return it as string
echo $dateTime->format("M d, Y H:m:i");
DateTime understands everything that strtotime does, so it can improve your solution. Try it out yourself, and let me know if this helps.
strtotime() can take 2 parameters. The first is the string and the second is the base timestamp.
Try the following:
// Assuming $startDateTime is a timestamp.
$followingDay = strtotime("next $repeatDay", $startDateTime);

Categories