Asked  7 Months ago    Answers:  5   Viewed   28 times

Having a nightmare at the moment and just can't see why it isn't working

I have a value in the form H:i (ie 10:00, 13:30) etc called $time

What I want to do is create two new values, $startTime which is 30 mins before $time and $endTime which is 30 mins after $time

I have tried the following but just doesn't seem to want to work

$startTime = date("H:i",strtotime('-30 minutes',$time));
$endTime = date("H:i",strtotime('+30 minutes',$time));

If I pass through 10:00 as $time and echo out both $startTime and $endTime I get:

$startTime = 00:30
$startTime = 01:30        

 Answers

85
$time = strtotime('10:00');
$startTime = date("H:i", strtotime('-30 minutes', $time));
$endTime = date("H:i", strtotime('+30 minutes', $time));
Wednesday, March 31, 2021
 
nomie
answered 7 Months ago
28

How can I make it return today's date in that case?

pseudocode:

if (today == monday)
    return today;
else
    return strtotime(...);

Btw, this trick also could work:

strtotime('last monday', strtotime('tomorrow'));
Wednesday, March 31, 2021
 
Terry
answered 7 Months ago
21

It might be related to bug #44073

You could try with something like this :

echo date("M", strtotime("-3 month", strtotime(date("F") . "1")) ) . "n";
echo date("M", strtotime("-2 month", strtotime(date("F") . "1")) ) . "n";
echo date("M", strtotime("-1 month", strtotime(date("F") . "1")) ) . "n";
echo date("M", time()) . "n";

(Solution found in the comments section of strtotime ; direct link)

And the output :

Apr
May
Jun
Jul

Kind of "cheating" with the date format and month's name and all that...

Wednesday, March 31, 2021
 
Valdas
answered 7 Months ago
29

Worked for me..

$timestamp = strtotime('10:09') + 60*60;

$time = date('H:i', $timestamp);

echo $time;//11:09

Explanation:

strtotime('10:09') creates a numerical timestamp in seconds, something like 1510450372. Simply add or remove the amount of seconds you need and use date() to convert it back into a human readable format.

$timestamp = strtotime('10:09') + 60*60; // 10:09 + 1 hour
$timestamp = strtotime('10:09') + 60*60*2; // 10:09 + 2 hours
$timestamp = strtotime('10:09') - 60*60; // 10:09 - 1 hour

time() also creates a numerical timestamp but for right now. You can use it in the same way.

$timestamp = time() + 60*60; // now + 1 hour
Saturday, July 17, 2021
 
SubniC
answered 4 Months ago
45

$timeoflastlogin is not a Unix timestamp but a string - so you can't use that as a $now parameter for strtotime. This should work:

$endtime = strtotime('+ 10 minutes', strtotime( $timeoflastlogin ) );

or easier:

$endtime = strtotime( $timeoflastlogin ) + 600; // 10 minutes == 600 seconds
Saturday, August 7, 2021
 
millenomi
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :