I'm looking for the next Thursday after a specific date, say 2014-02-25. The problem I'm having here is that when I use the below code, the time seems to be erased.
<?php
$timestamp = '2014-02-25 10:30:00';
echo date('Y-m-d H:i:s', strtotime("next Thursday", strtotime($timestamp)));
?>
The result I am getting is 2014-02-27 00:00:00 when I would like it to be 2014-02-27 10:30:00
Is there something I am missing here that is causing the time to be set to midnight?
I appreciate the help, thanks.
There is no time format that can directly express this. You need to produce a format like
next Thursday 10:30:00
... manually and pass that to strtotime(). The time information you need to extract from the reference time string. Like this:
$refdate = '2014-02-25 10:30:00';
$timestamp = strtotime($refdate);
echo date('Y-m-d H:i:s',
strtotime("next Thursday " . date('H:i:s', $timestamp), $timestamp)
);
The same results could be achieved using string concatenation:
echo date('Y-m-d', strtotime("next Thursday", $timestamp)
. ' ' . date('H:i:s', $timestamp);
The documentation for so called relative time formats can be found here
Related
I'm having a weird problem. When I do strtotime it's not considering the hours part of the original date, and it's always returning midnight. I tried to research but I couldn't find anything specific.
Is there something I'm missing?
$original_date = "2015-08-07 02:00:00";
$next_date = date('Y-m-d H:i:s', strtotime("monday this Week +1 week", strtotime($original_date)));
It returns $next_date as 2015-08-14 00:00:00
Try this, add time which you want to retrieve in next date,.
$original_date = "2015-08-07 02:00:00";
echo $next_date = date('Y-m-d H:i:s', strtotime("monday this Week 02:00:00 +1 week", strtotime($original_date)));
monday this week +1 week assumes you’re looking for midnight of the monday of the week of the passed in time. If you want to preserve the hours part of the time, then you can append it to your date format because it should always be the same as in $original_date
date('Y-m-d ' . date('H:i:s', strtotime($original_date)), strtotime("monday this Week +1 week", strtotime($original_date)));
When you use monday in strtotime you're resetting the time back to 00:00:00. You will have to explicitly pass the time in either your date or strtotime to get your desired behavior. See this same question for a similar issue.
$next_date = date('Y-m-d H:i:s', strtotime("monday this Week +1 week " . date('H:i:s', strtotime($original_date)), strtotime($original_date)))
$date = strtotime('2018-08-14 02:00:00');
$next_date = date('Y-m-d H:i:s', strtotime("monday this Week 02:00:00 +1 week", $date)); // 2018-08-20 02:00:00
I want to find the next occurrence of date from the current date.
For example, imagine I want to find 20th of the month from current date
If current date is 10th october then return the result 2014-10-20 (Y-m-d)
If current date is 22nd october then return the result 2014-11-20 (Y-m-d)
I created a solution just now using a while loop.
$oldd= "2014-06-20";
$newdate = date("Y-m-d",strtotime($oldd."+1months"));
while(strtotime($newdate) <= strtotime(date("Y-m-d")))
{
$newdate = date("Y-m-d",strtotime($newdate."+1months"));
}
echo $newdate;
manually and pass that to strtotime(). The time information you need to extract from the reference time string. Like this:
$refdate = '2014-02-25 10:30:00';
$timestamp = strtotime($refdate);
echo date('Y-m-d H:i:s',
strtotime("next Thursday " . date('H:i:s', $timestamp), $timestamp)
);
same results could be achieved using string concatenation:
echo date('Y-m-d', strtotime("next Thursday", $timestamp)
. ' ' . date('H:i:s', $timestamp);
another way, you can use methods of DateTime object, PHP has really rich API in dealing with date time.
$current_date = new DateTime('2014-06-20');
if ($current_date->format('d') >= 20) {
// $current_date->modify('last day of this month')->modify("+20 days");
$current_date->modify('first day of next month')->modify("+19 days");
}else{
$current_date->modify('first day of this month')->modify("+19 days");
}
echo $current_date->format("Y-m-d");
http://php.net/manual/en/datetime.modify.php
http://php.net/manual/en/datetime.formats.relative.php
I know that date("Y"), date("m") and date("d") will return current Year (2013), Month (07) and Date (11) respectively.
I am working with date Format: "2013-07-11". I have current date like this. Now I want to get the value "2013-06-11" and "2013-08-11" somehow using PHP.
What might be the code to get this values (Last Month's Same Date, and Next Month's Same Date)?
I tried:
$LastMonth = date ("m") - 1;
$LastDate = date("Y") . "-0" . $LastMonth . "-" . date("d");
But this will return error when it is October. In October it will show "2013-010-11".
What can be a better solution? Can anyone help me?
Use it with PHP's strtotime():
echo date('Y-m-d', strtotime('+1 month')); //outputs 2013-08-11
echo date('Y-m-d', strtotime('-1 month')); //outputs 2013-06-11
$date = new DateTime( "2013-07-11");
$date->modify("+1 month");
echo $date->format(‘l, F jS, Y’);
Try this
$lst_month=mktime(0,0,0,date('m')-1,date('d'),date('Y'));
echo "M<br>". date("Y-m-d",$lst_month);
$next_month=mktime(0,0,0,date('m')+1,date('d'),date('Y'));
echo "M<br>". date("Y-m-d",$next_month);
$nextmonth=date("dmy",strtotime("+1 month"));
$lastmonth=date("dmy",strtotime("-1 month"));
I'm trying to take a date object that's coming out of my Drupal CMS, subtract one day and print out both dates. Here's what I have
$date_raw = $messagenode->field_message_date[0]['value'];
print($date_raw);
//this gives me the following string: 2011-04-24T00:00:00
$date_object = date_create($date_raw);
$next_date_object = date_modify($date_object,'-1 day');
print('First Date ' . date_format($date_object,'Y-m-d'));
//this gives me the correctly formatted string '2011-04-24'
print('Next Date ' . date_format($next_date_object,'Y-m-d'));
//this gives me nothing. The output here is always blank
So I'm not understanding why the original date object is coming out fine, but then I'm trying to create an additional date object and modify it by subtracting one day and it seems like I can't do that. The output always comes out blank.
You can try:
print('Next Date ' . date('Y-m-d', strtotime('-1 day', strtotime($date_raw))));
date('Y-m-d',(strtotime ( '-1 day' , strtotime ( $date) ) ));
$date = new DateTime("2017-05-18"); // For today/now, don't pass an arg.
$date->modify("-1 day");
echo $date->format("Y-m-d H:i:s");
Using DateTime has significantly reduced the amount of headaches endured whilst manipulating dates.
Object oriented version
$dateObject = new DateTime( $date_raw );
print('Next Date ' . $dateObject->sub( new DateInterval('P1D') )->format('Y-m-d');
A one-liner option is:
echo date_create('2011-04-24')->modify('-1 days')->format('Y-m-d');
Running it on Online PHP Editor.
mktime alternative
If you prefer to avoid using string methods, or going into calculations, or even creating additional variables, mktime supports subtraction and negative values in the following way:
// Today's date
echo date('Y-m-d'); // 2016-03-22
// Yesterday's date
echo date('Y-m-d', mktime(0, 0, 0, date("m"), date("d")-1, date("Y"))); // 2016-03-21
// 42 days ago
echo date('Y-m-d', mktime(0, 0, 0, date("m"), date("d")-42, date("Y"))); // 2016-02-09
//Using a previous date object
$date_object = new DateTime('2011-04-24');
echo date('Y-m-d',
mktime(0, 0, 0,
$date_object->format("m"),
$date_object->format("d")-1,
$date_object->format("Y")
)
); // 2011-04-23
Online PHP Editor
Not sure why your current code isn't working but if you don't specifically need a date object this will work:
$first_date = strtotime($date_raw);
$second_date = strtotime('-1 day', $first_date);
print 'First Date ' . date('Y-m-d', $first_date);
print 'Next Date ' . date('Y-m-d', $second_date);
Answear taken from Php manual strtotime function comments :
echo date( "Y-m-d", strtotime( "2009-01-31 -1 day"));
Or
$date = "2009-01-31";
echo date( "Y-m-d", strtotime( $date . "-1 day"));
You can add strtotime() in date() with parameter number of day week or month.
Example for day:
date("Y-m-d", strtotime("-1 day"));
For week:
date("Y-m-d", strtotime("-1 week"));
For month:
date("Y-m-d", strtotime("-1 months"));
How about this: convert it to a unix timestamp first, subtract 60*60*24 (exactly one day in seconds), and then grab the date from that.
$newDate = strtotime($date_raw) - 60*60*24;
echo date('Y-m-d',$newDate);
Note: as apokryfos has pointed out, this would technically be thrown off by daylight savings time changes where there would be a day with either 25 or 23 hours
I want to get the date with specific day and time in PHP, like i want the date of next day and time of 9.30 am i.e "2011-06-02 09:30:00".
the code i was using get to do that,
<?php
$next_day_date = date("Y")."-".date("m")."-".(date("d")+1)." 09:30:00";
$new_trig_time_stamp = strtotime($next_day_date);
$trigger_date_time = date("Y-m-d H:i:s",$new_trig_time_stamp);
echo $trigger_date_time;
?>
the code above works fine but fails on 31 day, on 31st it returns "1970-01-01 05:30:00".
Is there any other way to do so.
When shifting dates by a fixed number, it's better to use mktime(), because it handles invalid dates well (e.g. it knows that January 32 is in fact February 1)
$trigger_date_time = date("Y-m-d H:i:s", mktime(9,30,0, date('n'), date('j')+1, date('Y'));
strtotime() is very useful here.
$trigger_date_time = date( "Y-m-d H:i:s", strtotime( "tomorrow 9:30" ) );
Calculate it via unix timestamp - much less annoyance
<?php
$trigger_date_time = date("Y-m-d 09:30:00",time() + 60*60*24);
echo $trigger_date_time;
?>
echo date('Y-m-d', strtotime('+1 day')) . ' 09:30:30';
Have a look at date_add.
In more detail, something like...
$myDate = new DateTime("Some time");
$myDate->add(new DateInterval("P1D"));
You can then use $myDate->format(…) to extract formatted representations.