I would like to add 5 days and current time to a date, which I have in string.
$date = new DateTime('2013-11-21');
date_add($date, date_interval_create_from_date_string('5 days'));
$curtime = date('H:i:s');
How to add current time to DateTime, or is there any other better way how to do it?
Just edit your last lane - I think it is the most objective solution to your problem. The rest of your code is correct.
$curtime = $date->format('Y-m-d H:i:s');
Remember that your second lane is just an alias to:
$date->add(DateInterval::createFromDateString('5 days'));
So the full code would be:
$date = new DateTime('2013-11-21');
$date->add(DateInterval::createFromDateString('5 days'));
$curtime = $date->format('Y-m-d H:i:s');
EDIT: I've just read your question again and you ask about adding current time to this date. If you want to add time, then you need to create it from current date. It's not the perfect solution, but I'm still working on it:
$now = new DateTime(date('1970-01-01 H:i:s'));
$date->add(DateInterval::createFromDateString($now->getTimestamp() . ' seconds'));
$curtime = $date->format('Y-m-d H:i:s');
echo $curtime;
EDIT2: I've corrected it much more, look at this code:
$date = new DateTime('2013-11-21');
$date->add(DateInterval::createFromDateString('5 days'));
$now = new DateTime('now');
$today = new DateTime(date('Y-m-d'));
$time = $today->diff($now);
$date->add($time);
echo $date->format('Y-m-d H:i:s');
EDIT3: And remember about time zones:
$date = new DateTime('2013-11-21', new DateTimeZone('Europe/Warsaw'));
$date->add(DateInterval::createFromDateString('5 days'));
$now = new DateTime('now', new DateTimeZone('Europe/Warsaw'));
$today = new DateTime(date('Y-m-d'), new DateTimeZone('Europe/Warsaw'));
$time = $today->diff($now);
$date->add($time);
echo $date->format('Y-m-d H:i:s');
And fiddle: http://sandbox.onlinephpfunctions.com/code/0080d18d18dd7e2fefa7dea7d961087f14ceb3df
You can add 5 days like this:
$nextX = time() + (5 * 24 * 60 * 60);
5 days * 24 hours * 60 mins * 60 secs
Try:
$date = '2013-11-21';
$date = strtotime($date);
$date = strtotime("+7 day", $date);
echo date('M d, Y', $date);
Try this
$hour_two = "2013-11-21";
$date = strtotime($hour_two);
$hour_two = $date + (5 * 24 * 60 * 60);
$hour_two=date('Y-m-d',$hour_two);
$currenttime = date('H:i:s');
$h = strtotime($currenttime);
$minute = date("i", $h);
$second = date("s", $h);
$hour = date("H", $h);
$new_time = $hour_two." ".$hour.":".$minute.":".$second;
echo $new_time."<br/>"; // here is your final date time
$timeto_string=strtotime($new_time); // test using strtotime
echo date('Y-m-d H:i:s',$timeto_string); // print by formating
$curtime = date('H:i:s');
$date = #date("Y-m-d H:i:s",strtotime($mydate.$curtime)) ;
Related
I have saved a date in mysql table in date('Y-m-d H:i:s') format
$blacklisted_date = "2018-07-22 17:57:24";
$blacklisted_days = 7;
$now = date('Y-m-d H:i:s');
I want to add $blacklisted_days to $blacklisted_date
$result_date = $blacklisted_date + $blacklisted_days;
and then want to find the difference in days between the $result_date and $now.
$diff_days = $result_date - $now;
I believe this code block will help you solve the problem.
$blacklisted_date = "2018-07-22 17:57:24";
$blacklisted_days = 7;
$now = date('Y-m-d H:i:s');
$result_date = date('Y-m-d H:i:s', strtotime($blacklisted_date . '+'. $blacklisted_days.' days'));
if( $result_date > $now ){
$datediff = strtotime($result_date) - strtotime($now);
}else{
$datediff = strtotime($now) - strtotime($result_date);
}
$diff_days = round( $datediff / (60 * 60 * 24));
You can do it easily using DateTime class, look here:
$blacklisted_date = "2018-07-22 17:57:24";
$blacklisted_days = 7;
$date1 = new DateTime($blacklisted_date); // blacklisted
$date1->add(new DateInterval("P{$blacklisted_days}D")); // add N days
$date2 = new DateTime(); // now
$interval = $date1->diff($date2); // get diff
echo $interval->days; // in days
I hope it's really clear to understand
I want to get the FRENCH GMT DATE from an Unix timestamp.
In my database, i saved the date in GMT+0 then i get the timestamp of this date and want to display the date with the good GMT+2
My timestamp is :1461857633 from database and it's equal to : 29/04/2016 12:27:11
And now i want to display this date with local GMT.
So i did this :
$timestamp = 1461857633;
$format = 'd/m/Y H:i:s';
$res = date($format, $timestamp);
echo $res;
and i have the same date 29/04/2016 12:27:11 where as my timezone is well 'Europe/Paris'
Normaly i should to have this date : 29/04/2016 14:27:11
$timestamp = 1461857633;
$effectiveDate = strtotime("+120 minutes", $timestamp);
$format = 'd/m/Y H:i:s';
$res = date($format, $effectiveDate);
echo $res;
date_default_timezone_set("UTC");
$HUTC = date("h");
date_default_timezone_set("Europe/Paris");
$HParis = date("h");
$diff = $HParis - $HUTC;
$timestamp = 1461857633;
$timestamp = 1461857633 + $diff * 60 * 60;
$format = 'd/m/Y H:i:s';
$res = date($format,$timestamp);
echo $res;
This will work both summer and winter time
You can simply add 2 hours to the timestamp or you can create a DateTime object and modify it by adding 2 hours to it:
$timestamp = 1461857633 + 2 * 60 * 60;
or
$dateTime = new DateTime();
$dateTime->setTimestamp(1461857633)->modify('+2 hours');
echo $dateTime->format('d/m/Y H:i:s');
or, another solution would be to calculate the seconds between the timezone you want to convert to and the Greenwich time zone (which is GMT+0) like:
$greenwichTimeZone = new DateTimeZone('Greenwich Mean Time');
$parisTimeZone = new DateTimeZone('Europe/Paris');
$dateTimeGreenwich = new DateTime('now', $greenwichTimeZone);
$seconds = $parisTimeZone->getOffset($dateTimeGreenwich);
$dateTime = new DateTime();
$dateTime->setTimestamp(1461857633 + $seconds);
echo $dateTime->format('d/m/Y H:i:s');
I want to send a reminder email.I don't want to use cron on Linux/Unix/BSD box or Scheduled Tasks on Windows.
I'm trying to subtract 15 minutes from the current time.
here is my code so far (doesn't work):
$days = date("j",time());
$months = date("n",time());
$years = date("Y",time());
$hours = date("G",time());
$mins = (date("i",time()));
$secs = date("s",time());
$mins = $mins-15;
To subtract 15 minutes from the current time, you can use strtotime():
$newTime = strtotime('-15 minutes');
echo date('Y-m-d H:i:s', $newTime);
Change the date into a timestamp (in seconds) then minus 15 minutes (in seconds) and then convert back to a date:
$date = date("Y-m-d H:i:s");
$time = strtotime($date);
$time = $time - (15 * 60);
$date = date("Y-m-d H:i:s", $time);
You can use DateInterval
$date = new DateTime();
$interval = new DateInterval("PT15M");
$interval->invert = 1;
$date->add($interval);
echo $date->format("c") . "\n";
you can try this as well,
$dateTimeMinutesAgo = new DateTime("15 minutes ago");
$dateTimeMinutesAgo = $dateTimeMinutesAgo->format("Y-m-d H:i:s");
How about substracting the 15 minutes from time() before converting it?
$time = time() - (15 * 60);
And then use $time instead of time() in your code.
$currentTime = date('Y-m-d H:i:s');
$before15mins = strtotime('-15 minutes');
echo date('Y-m-d H:i:s', $before15mins);
You can also use strtotime function to subtract days, hours and/or seconds from current time.
echo date('Y-m-d H:i:s', strtotime('-15 minutes'));
Following is the way you can add days / hours / minutes / sec to current time
$addInterval = date('Y-m-d H:i:s', strtotime("+$days days $hours hours $minute minute $sec second", strtotime(currentTime)));
You can also use DateInterval object
<?php
$date = new DateTime('Y-m-d H:i:s');
$date->sub(new DateInterval('PT10H30S'));
echo $date->format('Y-m-d H:i:s');?>
Try using
$min = time() - 900; //900 seconds = 15 minutes
To subtract 15 minutes you can do:
date('Y-m-d H:i:s', (time() - 60 * 15));
You can replace 15 with the number of minutes you want.
In case you're looking to subtract seconds you can simply do:
date('Y-m-d H:i:s', (time() - 10));
In this way you'll subtract 10 seconds.
If you have only time value than below will be useful
// Your time
$time = '12:15:00';
// Returned value '12:00:00'
$newTime = date('H:i:s', strtotime($time) - (15*60));
I know this question is outdated but i just want to share how i did it in easy way
$current = new DateTime("10 minutes ago", new DateTimeZone('Asia/Manila') );
echo $current->format("Y-m-d H:i:s");
//To Get Current DateTime
$currentDate = date("Y-m-d H:i:s");
//To Get Current DateTime - 15Min
$oldDate = date("Y-m-d H:i:s", strtotime($currentDate) - (15 * 60));
echo $currentDate;
echo $oldDate;
I have a PHP date in the form of 2013-01-22 and I want to get tomorrows date in the same format, so for example 2013-01-23.
How is this possible with PHP?
Use DateTime
$datetime = new DateTime('tomorrow');
echo $datetime->format('Y-m-d H:i:s');
Or:
$datetime = new DateTime('2013-01-22');
$datetime->modify('+1 day');
echo $datetime->format('Y-m-d H:i:s');
Or:
$datetime = new DateTime('2013-01-22');
$datetime->add(new DateInterval("P1D"));
echo $datetime->format('Y-m-d H:i:s');
Or in PHP 5.4+:
echo (new DateTime('2013-01-22'))->add(new DateInterval("P1D"))
->format('Y-m-d H:i:s');
$tomorrow = date("Y-m-d", strtotime('tomorrow'));
or
$tomorrow = date("Y-m-d", strtotime("+1 day"));
Help Link: STRTOTIME()
Since you tagged this with strtotime, you can use it with the +1 day modifier like so:
$tomorrow_timestamp = strtotime('+1 day', strtotime('2013-01-22'));
That said, it's a much better solution to use DateTime.
<? php
//1 Day = 24*60*60 = 86400
echo date("d-m-Y", time()+86400);
?>
echo date ('Y-m-d',strtotime('+1 day', strtotime($your_date)));
Use DateTime:
To get tomorrow from now :
$d = new DateTime('+1day');
$tomorrow = $d->format('d/m/Y h.i.s');
echo $tomorrow;
Results : 28/06/2017 08.13.20
To get tomorrow from a date :
$d = new DateTime('2017/06/10 08.16.35 +1day')
$tomorrow = $d->format('d/m/Y h.i.s');
echo $tomorrow;
Results : 11/06/2017 08.16.35
Hope it helps!
/**
* get tomorrow's date in the format requested, default to Y-m-d for MySQL (e.g. 2013-01-04)
*
* #param string
*
* #return string
*/
public static function getTomorrowsDate($format = 'Y-m-d')
{
$date = new DateTime();
$date->add(DateInterval::createFromDateString('tomorrow'));
return $date->format($format);
}
By strange it can seem it works perfectly fine: date_create( '2016-02-01 + 1 day' );
echo date_create( $your_date . ' + 1 day' )->format( 'Y-m-d' );
Should do it
here's working function
function plus_one_day($date){
$date2 = formatDate4db($date);
$date1 = str_replace('-', '/', $date2);
$tomorrow = date('Y-m-d',strtotime($date1 . "+1 days"));
return $tomorrow; }
$date = '2013-01-22';
$time = strtotime($date) + 86400;
echo date('Y-m-d', $time);
Where 86400 is the # of seconds in a day.
I want to add 5 minutes to this date: 2011-04-8 08:29:49
$date = '2011-04-8 08:29:49';
When I use strtotime I am always getting 1970-01-01 08:33:31
How do I add correctly 5 minutes to 2011-04-8 08:29:49?
$date = '2011-04-8 08:29:49';
$currentDate = strtotime($date);
$futureDate = $currentDate+(60*5);
$formatDate = date("Y-m-d H:i:s", $futureDate);
Now, the result is 2011-04-08 08:34:49 and is stored inside $formatDate
Enjoy! :)
Try this:
echo date('Y-m-d H:i:s', strtotime('+5 minutes', strtotime('2011-04-8 08:29:49')));
$expire_stamp = date('Y-m-d H:i:s', strtotime("+5 min"));
$now_stamp = date("Y-m-d H:i:s");
echo "Right now: " . $now_stamp;
echo "5 minutes from right now: " . $expire_stamp;
Results in:
2012-09-30 09:00:03
2012-09-30 09:05:03
$date = '2011-04-8 08:29:49';
$newDate = date("Y-m-d H:i:s",strtotime($date." +5 minutes"))
For adding
$date = new DateTime('2014-02-20 14:20:00');
$date->add(new DateInterval('P0DT0H5M0S'));
echo $date->format('Y-m-d H:i:s');
It add 5minutes
For subtracting
$date = new DateTime('2014-02-20 14:20:00');
$date->sub(new DateInterval('P0DT0H5M0S'));
echo $date->format('Y-m-d H:i:s');
It subtract 5 minutes
If i'm right in thinking.
If you convert your date to a unix timestamp via strtotime(), then just add 300 (5min * 60 seconds) to that number.
$timestamp = strtotime($date) + (5*60)
Hope this helps
more illustrative for simple and clear solution
$date = '2011-04-8 08:29:49';
$newtimestamp = strtotime($date. ' + 5 minute');//gets timestamp
//convert into whichever format you need
$newdate = date('Y-m-d H:i:s', $newtimestamp);//it prints 2011-04-08 08:34:49