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

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

Related

How to add timestamp to Minutes in 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

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;

php convert number into minutes

Say i have a variable like $speed = 5.5, what formula would I use to convert that into minutes, so in this case it would be 5 and a half minutes.
I need it to work in this:
date("Y-m-d H:i:s", strtotime("$now - $speed mins"));
Other examples, 2.25 would convert to 2 mins 15 secs, 7:75 to 7 mins 45 secs, etc
Anyone have any ideas? Never been a maths buff.
Just do it with second.
date('Y-m-d H:i:s', strtotime(sprintf('- %d second', $speed * 60)));
If you want more precision, then
date('Y-m-d H:i:s', strtotime(sprintf('- %d second', round($speed * 60))));
You could also use PHP's own DateInterval class (requires PHP 5.3) http://www.php.net/manual/en/dateinterval.createfromdatestring.php
With sample:
$interval = DateInterval::createFromDateString('5.5 minutes');
echo $interval->format('%Y-%m-%d %H:%i:%s')
Could also a Unix Timestamp for dates and 3600 = 1 hour.
For example, the current time would be: $timestamp = gmmktime();
So if you wanted to add ".5" (30 minutes) to the current time, you would say $timestamp + 1800. ".25" would be $timestamp + 900.
$minutes = floor($speed);
$seconds = ($speed - $minutes) * 60;

Create Variable in PHP Equal to Current Time Minus One Hour

In PHP, how could I create a variable called $livetime that equals the current time minus 1 hour?
Another way - without all the math and, in my opinion, reads better.
$hour_ago = strtotime('-1 hour');
If you're looking for how to display the time in a human readable format, these examples will help:
$livetime = date('H:i:s', time() - 3600); // 16:00:00
$livetime = date('g:iA ', time() - 3600); // 4:00PM
$livetime = time() - 3600; // 3600 seconds in 1 hour : 60 seconds (1 min) * 60 (minutes in hour)
See time PHP function for more information.
convert your date to strtotime and then subtract one hour from it
$now = date('Y-m-d H:i:s');
$time = strtotime($now);
$time = $time - (60*60); //one hour
$beforeOneHour = date("Y-m-d H:i:s", $time);
You could use the date_create function along with the date_sub function like I have shown here below: -
$currentTime = date_create(now());
$modifyTime = date_sub($currentTime,date_interval_create_from_date_string("1 hour"));
$liveTime = $modifyTime->format('Y-m-d H:i:s');
Assuming that a timestamp is fine you can use the time function like so
<?php
$livetime = time() - 60 * 60;
Current time is equal to time() (current time given in seconds after Unix epoch).
Thus, to calculate what you need, you need to perform calculation: time() - 60*60 (current time in seconds minus 60 minutes times 60 seconds).
$time_you_need = time() - 60*60;
First convert hours into seconds (3600) then use the following:
$your_date = date('F jS, Y',time() - 3600);

Get future timestamps

Would it be possible to get the unix timestamp 7 days from now?
Would be awesome!
Yes, get the unix timestamp and add 25200 to it. If you want to format that timestamp you can use date().
$future = time() + (60 * 60 * 24 * 7);
date("o", future);
And from the PHP docs for time()
date('Y-m-d', strtotime('+1 week'))
Sure.
Get the current timestamp. Add 7 days worth of seconds.
Note: The timestamp "7 days ahead" (in terms of 7 * 86400 seconds) of the current timestamp may not represent the same day-of-week or the same hour in the day (yay daylight savings!) or even the same second (rare, yay leap-seconds!).
time() + (60 * 60 * 24 * 7); // "good enough"
strtotime('+7 days'); // daylight savings save
Just add seven days?
$future = time() + 60*60*24*7;
// seconds ---^ ^ ^ ^
// minutes ---^ ^ ^
// hours ---^ ^
// days ---^
See time().... oh, the example given there does exactly what you want... I guess you have not read the manual before.
The proper way of doing this on a recent version of PHP is using the DateTime object (in my opinion).
$date = new DateTime('now'); // can be anyting else too
$date->modify('+1 week');
// PHP 5.3
$future = $date->getTimeStamp();
// PHP 5.2
$future = $date->format('U');
$now = time() + (7 * 24 * 60 * 60);
Get the current unix timestamp using time() and then multiply 60 seconds, 60 minutes, 24 hours, and 7 days.

Categories