How to add timestamp to Minutes in php - php

$date = 10/24/14
$time = 10:10
$mtc = 180 minutes
After using strtotime function i am getting timestamp,
echo strtotime('10/24/14 10:10');
timestamp is 1414138200
How can i add timestamp to 180 minutes. Can any one please help me.

If i'm understand your question well, you just can add the seconds to the current timestamp.
$timestamp = time();
$timestamp = $timestamp + 60*180; //180 minutes later

Unix timestamp is expresses in seconds. So to add 180 mins to it, just convert to secs and add to the current timestamp like
$ts = strtotime('10/24/14 10:10');
$ts += 180*60;
echo date("m/d/Y H:i",$ts);

Try this on
$time = strtotime('10/24/14 10:10');
$afterAddMinutesTime = strtotime('+180 minutes', $time);
echo $afterAddMinutesTime; // this is new timestamp after add 180 minutes
echo date('Y-m-d H:i:s', $afterAddMinutesTime); // just for checking

Related

PHP Current time in Hour:Minutes

I want to have have 2 variables with the current time in hours:minutes and also one that has +15 minutes. But first I must also add +6 hours.
So for example, right now it is 2018-01-07 16:35:10. So first I add +6 hours. So it will be 2018-01-07 22:35:10. Next, I want to extract only the hours:minutes.
I want to get only "22:35" to variable.
And next variable, I want 22:35 +15 minutes, so 22:50.
So I have $dateNow = 22:35 and $dateThen = 22:50
I have tried this so far to get current time now and +6 hours, but it's not working. Error: Call to a member function format() on integer
$now = strtotime(date("Y-m-d H:i:s")." +6 hours");
$then = $now->format('H:i');
echo $then;
i think in this case it would be very use full to use the DateTime class from PHP. The Problem with your code is strtotime returns a int not an DateTime object.
I've modified your code so it will work:
$org = new DateTime("2018-01-07 16:35:10");
$then = $org->add(new DateInterval("PT6H"));
echo $then->format("H:i"),"<br>";
$afterThen = $then->add(new DateInterval("PT15M"));
echo $afterThen->format("H:i");
Short solution with DateTime and DateInterval objects:
$now = new DateTime();
$result = $now->add(new DateInterval('PT6H15M'))->format('H:i');
Try this:
$time = time() + 3600 * 6; // add 6 hours
$date = date("H:i", $time); // format date
$date_plus_15 = date("H:i", $time + 60 * 15); // format date and add 15 minutes
echo "Time: {$date} <br>";
echo "Time + 15 mins: {$date_plus_15}";
Example here: https://ideone.com/ZR6cfy

How to Subtract Minutes

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;

Setting a time and date and adding to it in PHP

Basically am trying to set a time and a date in PHP then set a time gap which will range between minutes, loop through between a start time and end time echoing something out for each one. Have tried loads of different ways and cant seem to figure a way to set a date and add to it.
This seems the best script I have modified so far:
$minutes = 5;
$endtime = new DateTime('2012-01-01 09:00');
$newendtime = $endtime->format('Y-m-d H:i');
$timedate = new DateTime('2012-01-01 09:00');
while($stamp < $newendtime)
{
$time = new DateTime($timedate);
$time->add(new DateInterval('PT' . $minutes . 'M'));
$timedate = $time->format('Y-m-d H:i');
echo $timedate;
}
$minutes = 5;
$endtime = new DateTime('2012-01-01 09:00');
//modified the start value to get something _before_ the endtime:
$time = new DateTime('2012-01-01 8:00');
$interval = new DateInterval('PT' . $minutes . 'M');
while($time < $endtime){
$time->add($interval);
echo $time->format('Y-m-d H:i');
}
Do everything in seconds, and use php's time(), date(), and mktime functions.
In UNIX Time, dates are stored as the number of seconds since Jan 1, 1970.
You can render UNIX Timestamps with date().
$time = time(); // gets current time
$endtime = mktime(0,0,0, 1, 31, 2012); // set jan 31 # midnight as end time
$interval = 60 * 5; // 300 seconds = 5 minutes
while($time < $endtime){
$time += $interval;
echo date("M jS Y h:i:s a",$time) . "<br>"; // echos time as Jan 17th, 2012 1:04:56 pm
}
date reference:
http://us3.php.net/manual/en/function.date.php (includes superb date format reference too)
mktime reference: http://us2.php.net/mktime
time() only gets the current time, but just for kicks n' giggles: http://us2.php.net/time
And, it's super easy to store in a database!
This function will let you add date to your existing datetime. This will also preserves HH:MM:SS
<?php
function add_date($givendate,$day=0,$mth=0,$yr=0) {
$cd = strtotime($givendate);
$newdate = date('Y-m-d h:i:s', mktime(date('h',$cd),
date('i',$cd), date('s',$cd), date('m',$cd)+$mth,
date('d',$cd)+$day, date('Y',$cd)+$yr));
return $newdate;
}
?>
Usage:
add_date($date,12,0,0);
where $date is your date.

Can we use add time to the PHP date() function?

I am trying to make a schedule from the current hour
to 12 hours afterwards. I am using the date() function
to retrieve the current time but how can I increment the
hours and adjust the AM/PM? Can I just add 1 to the date()
function?
Thank you for your help.
$now = time();
$then = $now + 12 * 60 * 60;
echo date(format, $then);
You can use strtotime:
date('d-m-Y H:i:s', strtotime('+1 hour')); // one hour since now
or
date('d-m-Y H:i:s', strtotime('2011-02-25 14:00:42'));
$mydate = date("dateformat", time() + 43200);
time() gets the current timestamp, then you add 43200 which is 12 hours * 60 mins * 60 secs

PHP Date Time Current Time Add Minutes

Simple question but this is killing my time.
Any simple solution to add 30 minutes to current time in php with GMT+8?
I think one of the best solutions and easiest is:
date("Y-m-d", strtotime("+30 minutes"))
Maybe it's not the most efficient but is one of the more understandable.
This is an old question that seems answered, but as someone pointed out above, if you use the DateTime class and PHP < 5.3.0, you can't use the add method, but you can use modify:
$date = new DateTime();
$date->modify("+30 minutes"); //or whatever value you want
Time 30 minutes later
$newTime = date("Y-m-d H:i:s",strtotime(date("Y-m-d H:i:s")." +30 minutes"))
$timeIn30Minutes = mktime(idate("H"), idate("i") + 30);
or
$timeIn30Minutes = time() + 30*60; // 30 minutes * 60 seconds/minute
The result will be a UNIX timestamp of the current time plus 30 minutes.
echo $date = date('H:i:s', strtotime('13:00:00 + 30 minutes') );
13:00:00 - any inputted time
30 minutes - any interval you wish (20 hours, 10 minutes, 1 seconds etc...)
It looks like you are after the DateTime function add - use it like this:
$date = new DateTime();
date_add($date, new DateInterval("PT30M"));
(Note: untested, but according to the docs, it should work)
$dateTime = new DateTime('now', new DateTimeZone('Asia/Kolkata'));
echo $dateTime->modify("+10 minutes")->format("H:i:s A");
$ck=2016-09-13 14:12:33;
$endtime = date('H-i-s', strtotime("+05 minutes", strtotime($ck)));
In addition to Khriz's answer.
If you need to add 5 minutes to the current time in Mysql format you can do:
$cur_time=date("Y-m-d H:i:s");
$duration='+5 minutes';
echo date('Y-m-d H:i:s', strtotime($duration, strtotime($cur_time)));
time after 30 min, this easiest solution in php
date('Y-m-d H:i:s', strtotime("+30 minutes"));
for DateTime class (PHP 5 >= 5.2.0, PHP 7)
$dateobj = new DateTime();
$dateobj ->modify("+30 minutes");
The question is a little old, but I come back to it often ;p
Another way, which is also a one liner:
<?= date_create('2111-11-11 00:00:00')->modify("+30 minutes")->format('Y-m-d h:i:s') ?>
Or from timestamp, returns Y-m-d h:i:s:
<?= date_create('#'.time())->modify("+30 minutes")->format('Y-m-d h:i:s') ?>
Or from timestamp, returns timestamp:
<?= date_create('#'.time())->modify("+30 minutes")->format('U') ?>
new DateTime('+30minutes')
As simple as the accepted solution but gives you a DateTime object instead of a Unix timestamp.
$time = strtotime(date('2016-02-03 12:00:00'));
echo date("H:i:s",strtotime("-30 minutes", $time));

Categories