Welcome Always
Welcome Always

Reputation: 367

Echoing time from now and one hour later

Here I have set the my Centos time Zone.> sudo hwclock – show

Tue 04 Feb 2014 10:23:10 AM AFT  -0.596389 seconds
asia/kabul

Now on PHP I want to echo from 10:23:10 AM to 11:23:10 AM

Here I have set my code for the echoing.

<? echo (date('G', time())+5) ;
echo(':00 To ');
echo (date('G', time())+6);
echo(':00');  ?>

Now as result of my above echoing PHP code I get the result of

15:00 To 16:00

But instead I want to get the echo of below or as what ever my HTTP server time is from NOW to 1 hour next.

10:23:10 AM to 11:23:10 AM

Upvotes: 2

Views: 116

Answers (4)

Mohammad
Mohammad

Reputation: 572

You can use explode function.

<?PHP    
$time = date('G:i:s');
$eTime = explode(':', $time);
$timePlusOneHour = $eTime+1 . ":{$eTime[1]}:{$eTime[2]}";
echo $time . " To " . $timePlusOneHour;

Upvotes: -2

Zac
Zac

Reputation: 1072

echo date('Y-m-d H:i:s', strtotime('now')), ' to ', date('Y-m-d H:i:s', strtotime('+1 hour'));

For date format, please read: http://www.php.net/manual/en/function.date.php

For your format:

echo date('h:i:s A', strtotime('now')), ' to ', date('h:i:s A', strtotime('+1 hour'));

Upvotes: 2

Okonomiyaki3000
Okonomiyaki3000

Reputation: 3696

You should try strtotime(). Something like this:

echo date('h:i:s A'), ' to ', date('h:i:s A', strtotime('+1 hour'));

Upvotes: 2

Rossi
Rossi

Reputation: 26

Add to the time and then print the date.

echo date('G',time()+3600);

time() returns number of seconds since the unix epoch, then you add 3600 seconds for 1 hour and you use that in your date.

Upvotes: 1

Related Questions