How to get next Date with specific time in PHP - php

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.

Related

get strtotime of specific time in php

I want to get the timestamp of a day/time for eg
17/12/2014 8pm
Currently I am doing
$curtime = strtotime(date("Y-m-d H:i:s"));
which is giving me the current timestamp but I need to be of 8pm.
Any help is highly appreciated. Thanks in advance.
If you're trying to get a timestamp of today at 8pm, it's actually much more simple than using date since you can use relative times in a strtotime:
$curtime = strtotime('today 8pm');
If you test this with date:
echo date('Y-m-d H:i:s', $curtime); // Returns 2014-12-17 20:00:00
Here's a whole writeup on relative date formats that explains how you can construct proper relative dates.
The best way to do this is using date_create_from_format. Checking out format's parameters, you will end up with something like this:
$date = date_create_from_format('d/m/Y ga', '17/12/2014 8pm');
if (!empty($date)) {//returns false if can't create date
$timestamp = $date->getTimestamp();
//echo date('d/m/Y H:i:s', $timestamp);
}

Add days to a timestamp

Im trying to add a certain amount of days to a timestmp using this in PHP:
$capturedDate = '2008-06-20';
$endDate = strtotime($capturedDate);
$endDate2 = strtotime('+1 day',$endDate);
echo $endDate2;
but its displaying: 1216526400
any ideas?
Try:
echo date("Y-m-d H:i:s",$endDate2);
Or (for just the date):
echo date("Y-m-d",$endDate2);
You can find documentation about how to format your string here: http://php.net/manual/en/function.date.php
You should be using DateTime for working with dates. It's timezone friendly.
$datetime = new DateTime('2008-06-20');
$datetime->modify('+1 day');
echo $datetime->getTimestamp();
strtotime() converts the date into a unix timestamp which is the number of seconds since January 1st 1970. If you want a date output you have to run the finished timestamp through date() first.
$capturedDate = '2008-06-20';
$endDate = strtotime($capturedDate.' +1 day');
echo date("Y-m-d", $endDate);
strtotime creates a Unix timestamp so if you want to be presented with a formatted date, you need to pass the timestamp as an argument to the date function as follows:
$capturedDate = '2008-06-20';
$endDate = strtotime($capturedDate);
$endDate2 = strtotime('+1 day',$endDate);
echo date('Y-m-d', $endDate2);
Additionally, there are a wide variety of parameters you can use in the date function if you want to display additional information.
e.g.: echo date('Y-m-d H:i:s', $endDate2); or echo date('Y-m-d h:i:s a', $endDate2);, etc.
Sooooo close, just take your timestamp and convert it back into date format using date("desired format",$endDate2);
DateTime is a very nice way to deal with dates. You can try like this:
$capturedDate = '2008-06-20';
$date = DateTime::createFromFormat('Y-m-d', $capturedDate)->modify('+1 day');
echo $date->getTimestamp();

Add minutes to current time

I am trying to add minutes to current date but it returns strange results
date_default_timezone_set('Asia/Karachi');
$currentDate = date("m-d-Y H:i:s");
$currentDate_timestamp = strtotime($currentDate);
$endDate_months = strtotime("+10 minutes", $currentDate_timestamp);
$packageEndDate = date("m-d-Y H:i:s", $endDate_months);
echo " <br> " . $packageEndDate . " <br> ";
echo $currentDate;
I am getting Output
01-01-1970 05:50:00
07-19-2013 20:25:23
It should return
07-19-2013 20:35:23
07-19-2013 20:25:23
After this I need to query to database so date format should be same. Database column is of string type.
Your code is redundant. Why format a timestamp as a string, then convert that string back to a timestamp?
Try
$now = time();
$ten_minutes = $now + (10 * 60);
$startDate = date('m-d-Y H:i:s', $now);
$endDate = date('m-d-Y H:i:s', $ten_minutes);
instead.
Probably the minimalist way would be:
date_default_timezone_set('Asia/Baku');
$packageEndDate = date('Y-m-d H:i:s', strtotime('+10 minute'));
echo $packageEndDate;
Output (Current time in my city at the time of writing):
2017-07-20 12:45:17
Try this:
$now = time();
$tenMinFromNow = date("m-d-Y H:i:s", strtotime('+10 minutes', $time));
$tenMinsFromNow = (new \DateTime())->add(new \DateInterval('PT10M'));
Will leave you with a DateTime object representing a time 10 minutes in the future. Which will allow you to do something like:-
echo $tenMinsFromNow->format('d/m/Y H:i:s');
See it working
PHP version >= 5.4 I'm afraid, but you should be using at least that version by now anyway.
Pakistan, which is the localisation explicitly set, uses "DD-MM-YYYY" format dates so the problem occurs when you cast the date into a string of "MM-DD-YYYY". This American format of date is not parseable by the Pakistan localisation.
If you still want to keep the round-trip to a string and back, use DD-MM-YYYY or the ISO datetime format.
While this is the only (current) answer which actually explains your original issue, I recommend the code be refactored as others have demonstrated.

add day to current date

add a day to date, so I can store tomorrow's date in a variable.
$tomorrow = date("Y-m-d")+86400;
I forgot.
date returns a string, whereas you want to be adding 86400 seconds to the timestamp. I think you're looking for this:
$tomorrow = date("Y-m-d", time() + 86400);
I'd encourage you to explore the PHP 5.3 DateTime class. It makes dates and times far easier to work with:
$tomorrow = new DateTime('tomorrow');
// e.g. echo 2010-10-13
echo $tomorrow->format('d-m-Y');
Furthermore, you can use the + 1 day syntax with any date:
$xmasDay = new DateTime('2010-12-24 + 1 day');
echo $xmasDay->format('Y-m-d'); // 2010-12-25
date() returns a string, so adding an integer to it is no good.
First build your tomorrow timestamp, using strtotime to be not only clean but more accurate (see Pekka's comment):
$tomorrow_timestamp = strtotime("+ 1 day");
Then, use it as the second argument for your date call:
$tomorrow_date = date("Y-m-d", $tomorrow_timestamp);
Or, if you're in a super-compact mood, that can all be pushed down into
$tomorrow = date("Y-m-d", strtotime("+ 1 day"));
Nice and obvious:
$tomorrow = strtotime('tomorrow');
You can use the add method datetime class.
Eg, you want to add one day to current date and time.
$today = new DateTime();
$today->add(new DateInterval('P1D'));
Further reference php datetime add
Hope this helps.
I find mktime() most useful for this sort of thing. E.g.:
$tomorrow=date("Y-m-d", mktime(0, 0, 0, date("m"), date("d")+1, date("Y")));

php get future date time

I don't know how to explain this correctly but just some sample for you guys so that you can really get what Im trying to say.
Today is April 09, 2010
7 days from now is April 16,2010
Im looking for a php code, which can give me the exact date giving the number of days interval prior to the current date.
I've been looking for a thread which can solve or even give a hint on how to solve this one but I found none.
If you are using PHP >= 5.2, I strongly suggest you use the new DateTime object, which makes working with dates a lot easier:
<?php
$date = new DateTime("2006-12-12");
$date->modify("+7 day");
echo $date->format("Y-m-d");
?>
Take a look here - http://php.net/manual/en/function.strtotime.php
<?php
// This is what you need for future date from now.
echo date('Y-m-d H:i:s', strtotime("+7 day"));
// This is what you need for future date from specific date.
echo date('Y-m-d H:i:s', strtotime('01/01/2010 +7 day'));
?>
The accepted answer is not wrong but not the best solution:
The DateTime class takes an optional string in the constructor, which can define the same logic as the modify method.
<?php
$date = new DateTime("+7 day");
For example:
<?php
namespace DateTimeExample;
$now = new \DateTime("now");
$inOneWeek = new \DateTime("+7 day");
printf("Now it's the %s", $now->format('Y-m-d'));
printf("In one week it's the %s", $inOneWeek->format('Y-m-d'));
For a list of available relative formats (for the DateTime constructor) take a look at http://php.net/manual/de/datetime.formats.relative.php
If you are using PHP >= 5.3, this could be an option.
<?php
$date = new DateTime( "2006-12-12" );
$date->add( new DateInterval( "P7D" ) );
?>
You will have to look into strtotime(). I'd imagine your final code would look something like this:
$future_date = "April 16,2010";
$seconds = strtotime($future_date) - time();
$days = $seconds /(60 * 60* 24);
echo $days; //Returns "6.0212962962963"
You can use mktime with date. (http://php.net/manual/en/function.date.php)
Date gives you the current date. This is better than simply adding/subtracting to a timestamp since it can take into account daylight savings time.
<?php
# this gets you 7 days earlier than the current date
$lastWeek = mktime(0, 0, 0, date("m") , date("d")-7, date("Y"));
# now pretty-print it out (eg, prints April 2, 2010.)
echo date("F j, Y.", $lastWeek), "\n";
?>

Categories