This might be fairly easy but can't seem to figure it out.
This is what I want to get;
$t = 12:00
$todaysdate = 2015-11-30
I want to get $tomorrowsdate with $t appended to it
$tomorrowsdate = 2015-12-01 12:00
So basically I want to be able to add a given time to next day.
It's important to define them as a string if you wanna use strtotime. Otherwise, you will get freaky results e.g. the year 1970 ;)
$t = '12:00';
$todaysdate = '2015-11-30';
$tomorrowsdate = date('Y-m-d', strtotime("{$todaysdate} + 1 day")) . " {$t}";
echo $tomorrowsdate;
Incase you want to use object oriented style use the following;
$today = new DateTime(); //today (2015-12-29)
$tommorow = $today->add(new DateInterval('P1D')); // add one day
echo $tommorow->format('Y-m-d H:i'); // echo results in desired format eg. 2015-12-30 12:36
// incase that 12:00 is important..
$tommorow->setTime(12,0);
echo $tommorow->format('Y-m-d H:i'); // 2015-12-30 12:00
Related
I have a PHP DateTime object that looks like this...
$startdate = '01/05/2019';
$mydate = DateTime::createFromFormat("d/m/Y", $startdate);
Is there a way to advance this date to the nearest 17th of the month? The start date will be dynamic so I cant hardcode it.
Does anybody have an example?
Add one day to your date until you find the next 17th.
<?php
$startdate = '2019-01-05';
$interval = new DateInterval('P1D'); // define interval as 1 day
$date = new DateTime($startdate);
while($date->format('j') != 17){
$date->add($interval);
}
echo $date->format('Y-m-d') . "\n";
Output: 2019-01-17
The trick here is to find the first date of the appropriate month, then add sixteen days.
These modify() expressions are helpful.
$mydate->modify ("first day of this month")->modify("+ 16 days");
$mydate->modify ("first day of next month")->modify("+ 16 days");
Next, you can figure out which one you want:
if ($mydate->format("d") > 17) ...
Put it all together like this:
$mydate = DateTime::createFromFormat("d/m/Y", $startdate);
if ($mydate->format("d") > 17) {
$mydate->modify ("first day of this month")->modify("+ 16 days");
}
else {
$mydate->modify ("first day of next month")->modify("+ 16 days");
echo $mydate->format('Y-m-d') . "<br/>\r\n";
And, when you test this sort of thing, be sure to test it with days in December and February to ensure the year-rollover and month-rollover logic works correctly.
I would like to find the date stamp of monday, tuesday, wednesday, etc. If that day hasn't come this week yet, I would like the date to be this week, else, next week. Thanks!
See strtotime()
strtotime('next tuesday');
You could probably find out if you have gone past that day by looking at the week number:
$nextTuesday = strtotime('next tuesday');
$weekNo = date('W');
$weekNoNextTuesday = date('W', $nextTuesday);
if ($weekNoNextTuesday != $weekNo) {
//past tuesday
}
I know it's a bit of a late answer but I would like to add my answer for future references.
// Create a new DateTime object
$date = new DateTime();
// Modify the date it contains
$date->modify('next monday');
// Output
echo $date->format('Y-m-d');
The nice thing is that you can also do this with dates other than today:
// Create a new DateTime object
$date = new DateTime('2006-05-20');
// Modify the date it contains
$date->modify('next monday');
// Output
echo $date->format('Y-m-d');
To make the range:
$monday = new DateTime('monday');
// clone start date
$endDate = clone $monday;
// Add 7 days to start date
$endDate->modify('+7 days');
// Increase with an interval of one day
$dateInterval = new DateInterval('P1D');
$dateRange = new DatePeriod($monday, $dateInterval, $endDate);
foreach ($dateRange as $day) {
echo $day->format('Y-m-d')."<br />";
}
References
PHP Manual - DateTime
PHP Manual - DateInterval
PHP Manual - DatePeriod
PHP Manual - clone
The question is tagged "php" so as Tom said, the way to do that would look like this:
date('Y-m-d', strtotime('next tuesday'));
For some reason, strtotime('next friday') display the Friday date of the current week. Try this instead:
//Current date 2020-02-03
$fridayNextWeek = date('Y-m-d', strtotime('friday next week'); //Outputs 2020-02-14
$nextFriday = date('Y-m-d', strtotime('next friday'); //Outputs 2020-02-07
You can use Carbon library.
Example: Next week friday
Carbon::parse("friday next week");
PHP 7.1:
$next_date = new DateTime('next Thursday');
$stamp = $next_date->getTimestamp();
PHP manual getTimestamp()
Sorry, I didn't notice the PHP tag - however someone else might be interested in a VB solution:
Module Module1
Sub Main()
Dim d As Date = Now
Dim nextFriday As Date = DateAdd(DateInterval.Weekday, DayOfWeek.Friday - d.DayOfWeek(), Now)
Console.WriteLine("next friday is " & nextFriday)
Console.ReadLine()
End Sub
End Module
If I understand you correctly, you want the dates of the next 7 days?
You could do the following:
for ($i = 0; $i < 7; $i++)
echo date('d/m/y', time() + 86400 * $i);
Check the documentation for the date function for the format you want it in.
if you want to find Monday then 'dayOfWeek' is 1 if it is Tuesday it will be 2 and so on.
var date=new Date();
getNextDayOfWeek(date, 2);
// this is for finding next tuesday
function getNextDayOfWeek(date, dayOfWeek) {
// Code to check that date and dayOfWeek are valid left as an exercise ;)
var resultDate = new Date(date.getTime());
resultDate.setDate(date.getDate() + (7 + dayOfWeek - date.getDay()) % 7);
return resultDate;
}
Hope this will be helpfull to you, thank you
The PHP documentation for time() shows an example of how you can get a date one week out. You can modify this to instead go into a loop that iterates a maximum of 7 times, get the timestamp each time, get the corresponding date, and from that get the day of the week.
What I am doing is actually iterating from 8AM to 9PM with 1 hour interval but it doesn't give me correct result. This is my code below
<?php
$date = date("08:00");
for($i=1; $i<=10; $i++)
{
$date = strtotime("+".$i." hour");
echo date('H:i A', $date);
}
?>
it starts with current time!,
Thanks for your help.
There are a few different problems to unpack here:
$date = date("08:00");
This isn't useful. date is for formatting a timestamp - you can't use it to create one from an hours:minutes string like this.
What you probably want is
$date = strtotime("08:00");
The reason this didn't cause any problems is that inside your loop you then immediately overwrite it with:
$date = strtotime("+".$i." hour");
This will create a new timestamp, but it will be relative to now, not your initial time. Saying "+1 hour" means you want a timestamp that is now + 1 hour. You can use strtotime to create a timestamp relative to another by using the second argument...
$date = strtotime("+{$i} hour", $date);
...but you're still overwriting your start value. So let's put that in a variable called $start instead:
$start = strtotime("08:00");
...
$date = strtotime("+".$i." hour", $start);
This gets you:
<?php
$start = strtotime("08:00");
for($i=0; $i<=10; $i++) {
$date = strtotime("+{$i} hour", $start);
echo date('H:i A', $date), PHP_EOL;
}
which will output:
08:00 AM
09:00 AM
10:00 AM
...
18:00 PM
It doesn't quite get you to 9PM, because that's not ten hours away from 8am, but 13. I'll leave the last part to you.
See https://eval.in/867974
Also, you might want to look at PHP's DateTime library, which gives you an object-oriented interface for working with dates & times. For a simple script like this, it's not vital.
Hi Suppose I have a timestamp of "2005-10-16 13:05:41".
How would I go about creating a variable that will have a unixtime of the next time it becomes 10am from that initial point?
Would it be something like this?
$timestamp = "2005-10-16 13:05:41";
$tenAMTime = strtotime("next 10am", $timestamp);
I am guessing there is some string I can use to do this? Like "next thursday" example in the PHP documentation.
You nearly had it...
$tomorrowAt10Am = strtotime('+1 day 10:00:00', $timestamp);
Edit:
This was based on the title of your question, for the timestamp of 10am the next day. If you want to output 10am the same day for any times before 10am then you'll want to add some extra logic, as thatidiotguy suggested.
Edit2:
For some reason it won't work if you put all the logic in the same strtotime method, so I made a simple function. You could easily put this into a single line, but I left it as 2 to make it clearer:
$time1 = strtotime('-2 days 09:59:59');
$time2 = strtotime('-2 days 10:00:01');
function next_10am($time)
{
$temp = strtotime('+1 day -10 hours', $time);
return strtotime('10:00', $temp);
}
echo next_10am($time1); // Outputs: 2012-09-08 10:00:00
echo next_10am($time2); // Outputs: 2012-09-09 10:00:00
There is no way for strtotime to know whether or not 10am has already passed, so this is how I would do it:
$timestamp = strtotime("2005-10-16 13:05:41");
// Get current hour and if it is > 10 add a day
if (date('G',$timestamp) >= 10) {
$tenAMTime = strtotime("+1 day 10am", $timestamp);
}
else {
$tenAMTime = strtotime("10am", $timestamp);
}
echo date('r',$tenAMTime); // Comment this out if you want
I am taking in a parameter that is a date in PST timezone which will be in the format of "YYYY-MM-DD" (e.g. "2011-08-15"). This parameter is optional. I have 2 questions that I've been struggling with.
I need to calculate the start and end datetime in UTC for this date.
So if the inputted date is 2011-08-15, I want to get the start and end datetimes:
2011-08-15 07:00:00
2011-08-15 06:59:59
(These are essentially the beginning and end of day)
Second, is to handle the case when the date is not passed in. I want to default to the current PST date them and start from there. So if the current datetime is 2011-08-01 10:00:00, I want to get the same start and end datetimes similar to the first scenario except it's based on the inputted date.
2011-08-01 07:00:00
2011-08-01 06:59:59
I've been pulling my hair out dealing with date and datetime conversions. I'm sure I'm missing something super straightforward.
Parse the date and assume PST timezone:
$date = new DateTime("2011-08-15", new DateTimeZone("PST"));
Change the timezone to UTC: (this does the all conversions for you)
$date->setTimeZone(new DateTimeZone("UTC"));
Calculate start and end. Start is our $date and end is $date + 1 day
$start = $date;
$end = clone $date;
$end->modify("+1 day"); // now $end is $start + 1 day
Print start/end:
printf("start: %s, end: %s\n", $start->format('Y-m-d H:i:s'), $end->format('Y-m-d Hi:s'));
// this prints start: 2011-08-15 07:00:00, end: 2011-08-16 07:00:00
For the last part of your questions, you can easily compare two dates:
if ($date > new DateTime()) { // if $date is after now
// do something
}
So you could do something like that:
if ($date > new DateTime()) {
$date->setTimeZone(new DateTimeZone("UTC"));
}
If you don't much like the OO syntax you could also use the function aliases:
$date = date_create(...);
date_format($date, ...);
date_modify($date, ...);
// ...
Use setTimezone function
Working example (I'm not in PST timezone, so I have to set it explicitly)
$date_input = "20011-09-15";
//$date_input = null; //That will emulate no-input case
date_default_timezone_set("America/Los_Angeles"); //if you are in PST, you don't need this line
$date_start = new DateTime($date_input);
$date_end = new DateTime($date_input);
$date_end->modify("+1 day");
/*$date_start->setTimezone(new DateTimeZone("America/Los_Angeles"));
$date_end->setTimezone(new DateTimeZone("America/Los_Angeles"));*/
//end of date is equal to start of the next day.
//But, if you need something like 2011-08-11 23:59:59 add $date_end->modify('-1 second')
$date_start->setTime(0,0,0);
$date_end->setTime(0,0,0);
echo "Date Start PST:".$date_start->format("Y-m-d H:i:s")."<br/>";
echo "Date End PST:".$date_end->format("Y-m-d H:i:s")."<br/>";
//UTC is equal to London time. Almost :)
$date_start->setTimezone(new DateTimeZone ('Europe/London'));
$date_end->setTimezone(new DateTimeZone ('Europe/London'));
echo "Date Start UTC:".$date_start->format("Y-m-d H:i:s")."<br/>";
echo "Date End UTC:".$date_end->format("Y-m-d H:i:s")."<br/>";