5 minutes and 10 seconds before given time [duplicate] - php

This question already has answers here:
get next and previous day with PHP
(11 answers)
Closed 7 years ago.
I need to figure out
5 minutes before given time
10 seconds before given time
I have try this code
echo $time.'<br />';
echo 'fivemin'.$fivemin= date("H:m:s",strtotime("-5 minutes",strtotime($time)));
echo '<br />tensec'.$tensec=$datetime_from = date("H:m:s",strtotime("-10 seconds",strtotime($time)));
result
15:55:44
fivemin 15:04:44
tensec 15:04:34

You can use the DateTime::sub method for this.
// Set the initial date/time
$date = new DateTime('2015-04-03 12:00:00');
// Go back 5 minutes
$date->sub(new DateInterval('PT5M'));
// $date is now 2015-04-03 11:55:00, go back another 10 seconds...
$date->sub(new DateInterval('PT10S'));
You can also combine the two subs into one (I just split them so you have a better idea of what's going on and you may want to run some actions in between the subs):
$date->sub(new DateInterval('PT5M10S')); // 5 minutes and 10 seconds ago
That will subtract 5 minutes and 10 seconds in 1 go.
You will then end up with $date being a DateTime object, that is now representing 2015-04-03 11:54:50.

Related

How to get next hour's 15th minute of time from given time in PHP [duplicate]

This question already has answers here:
Adding minutes to date time in PHP
(13 answers)
Closed 1 year ago.
Is there a way of getting the next hour's 15th minutes from given time?
For Example
if input is 2021-08-26 12:00:37 then output should be 2021-08-26 12:15:00
if input is 2021-08-26 12:30:37 then output should be 2021-08-26 13:15:00
Use Carbon
It is installed by default in Laravel.
$date = Carbon::createFromFormat('Y-m-d H:i:s', '2021-08-26 12:00:37');
$newDate = $date->addMinutes(15)->format('Y-m-d H:i:s');
You can convert the date to a timestamp and then add it the equivalent of 15 minutes in seconds (15 * 60) and then convert it back to a date
I think this is what you looking for...
$start = '2018-05-21 20:24:45';
echo date('Y-m-d H:i:s',strtotime('+15 minutes',strtotime($start)));

PHP - Resetting Stats

I'm using a WordPress plugin that re-sets its stats every 7 days using the following line of code:
$keep_time = 60*60*24*7; // 7 days for now (TODO: admin setting)
Could someone help me to modify the code to re-set the stats every 6 hours or every other day?
I did try to try to change the 7 to 1 but it doesn't work. Probably the solution is very simple, but unfortunately I'm not a PHP programmer.
Thanks everyone for answering my question, wanted to give a vote but I don't have enough 'reputation'
For 6 hours use:
$keep_time = 60*60*6;
For 2 days use:
$keep_time = 60*60*24*2;
The value is in seconds. 60*60 is the number of seconds in an hour. Then you multiply by the number of hours you want. If you want multiple days, you multiply by 24 hours in a day, and then the number of days.
I like DateTime() and DateInterval() for this. Not only is it clearer but it handles daylight savings time and leap years as well as those pesky last days of the month.
7 Days:
$start_time = new DateTime(); // "now" as an example
$keep_time = new DateInterval('P7D'); // 7 days
$start_time->add($keep_time);
echo $start_time->format('Y-m-d');
6 Hours
$start_time = new DateTime(); // "now" as an example
$keep_time = new DateInterval('PT6H'); // 6 hours
$start_time->add($keep_time);
echo $start_time->format('Y-m-d');
$keep_time = 60(sec)*60(min)*24(hours)*7(days);
you need to do
$keep_time = 60*60*6;

PHP - Get timestamp X hours from now [duplicate]

This question already has answers here:
php string in a date format, add 12 hours
(4 answers)
Closed 8 years ago.
I'd like to take a number of hours imputed in a text box and get a timestamp back so I can create a countdown timer.
The countdown is fine so ignore how that is done etc. But whats the best way to get a timestamp '48' hours from now. For example user has entered 48?
$hours = 48;
$timestamp = (new DateTime())->modify("+{$hours} hours")->format('U');
You can create a timestamp like this:
<?php
strtotime(date('d-m-Y H:i:s') . "+ 48 hours");
?>
Your looking for strtotime. For example, you can just use strtotime('+48 hours'), and it will return a unix timestamp for that time.

DIFF from two dates and get hours and minutes [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Time diff in minutes between 2 dates
php date_diff in hours
i have:
$datetime1 = new DateTime('2012-12-01 10:40:00');
$datetime2 = new DateTime('2012-12-03 12:00:00');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%1 day %h hours %i minutes');
this working ok, but how can i show only hours and minutes? For this example should be:
49 hours 10 minutes
From the PHP manual:
Note:
The DateInterval::format() method does not recalculate carry over points in time strings nor in date segments. This is expected because it is not possible to overflow values like "32 days" which could be interpreted as anything from "1 month and 4 days" to "1 month and 1 day".
In other words, the DateInterval class won't do what you're asking for on its own; you'd have to do the calculation yourself.
Something like this should do it:
$hours = $interval->h + ($interval->d*24);
print "{$hours} hours ".$interval->format('%i minutes');

PHP Timestamp display seconds [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Converting timestamp to time ago in php e.g 1 day ago, 2 days ago
I'm trying to display how old the post is through php. What I'm doing is storing in mysql the php timestamp using the code below.
$timestamp=time();
So then I'm left with a number like this
1334216865
How would I go about changing it so it will say example:
8 seconds ago
If you refresh the page 10 seconds later it will show:
18 seconds ago
I'm not sure how I would convert a timestamp back into time to show my examples.
Thank you
Or you could make a little "time_ago" function:
function ago($time) {
$timediff=time()-$time;
$days=intval($timediff/86400);
$remain=$timediff%86400;
$hours=intval($remain/3600);
$remain=$remain%3600;
$mins=intval($remain/60);
$secs=$remain%60;
if ($secs>=0) $timestring = "0m".$secs."s";
if ($mins>0) $timestring = $mins."m".$secs."s";
if ($hours>0) $timestring = $hours."u".$mins."m";
if ($days>0) $timestring = $days."d".$hours."u";
return $timestring;
}
echo ago(1334214962);
echo (time() - $post_timestamp) . " seconds ago";
very simple.
Just run the time() function again and subtract the posted time - then do some arithmetic to output how long it has been: 60 seconds per minute, 60 minutes per hour,etc... the time function is how many seconds since some event a long time ago. so, every increment is 1 second on the time function.

Categories