I have an working javascript code that i use on an webpage to set text of an button. The text of the button show the time in 24 hour format with added 1 hour and 30 minutes rounded of to nearest 5 minutes. As i need to use the resulting time in an php code snippet on button click i need to convert my code to php syntax to get the same result.
As example if time now is 15:17 the result should be 16:50
<SCRIPT>
var today = new Date();
today.setHours(today.getHours() + 1);
today.setMinutes(today.getMinutes() + 30);
var coeff = 1000 * 60 * 5;
var date = "09:07:00"; //or use any other date
var rounded = new Date(Math.round(today.getTime() / coeff) * coeff)
var tid_1hour30min_from_now = rounded.toLocaleTimeString([], {hour: '2-digit', minute:'2-digit'});
var tid_hour = rounded.getHours();
var tid_minutes = rounded.getMinutes();
var ater = "Åter"
var result1hour30minfromnow = ater +" "+ tid_1hour_from_now
</SCRIPT>
So i would like to do the same but in php.
Been trying with simular code to this:
$startTime = date("Y-m-d H:i:s");
$cenvertedTime = date('Y-m-d H:i:s',strtotime('+1,5
hour',strtotime($startTime)));
$current_time = strtotime($cenvertedTime);
$frac = 900;
$r = $current_time % $frac;
$new_time = $current_time + ($frac-$r);
$new_date = date('Y-m-d g:i:s A', $new_time);
But this doesn't turn out well, i newer get the same time in both script above. How can i translate the javascript as close as possible to php?
/ Kristian
It can be done with "normal" date() function also.
$startTime = strtotime(date("Y-m-d H:i:s"))+90*60; // add 90 minutes times 60 seconds to UNIX time.
$NewMinute = round(date("i", $startTime)/5)*5; // find new minute by rounding on "5"
echo date("Y-m-d H:", $startTime) .str_pad($NewMinute, 2, "0", STR_PAD_LEFT);
So if you want the same time value of your button in the php script on button click then you can simply send the time value using ajax to the php file
To use JQuery plugin include below line
<script src="http://code.jquery.com/jquery-latest.js"></script>
then use below code to send time value using ajax to php file on button click
var btn_date = $('#btn_id').text();
$.ajax({
url: "phpfile.php",
type: "post",
data: "date_val="+btn_date,
success: function(data) {
$('.status_text').html(data);
}
});
If I read the code correctly you want to add 1 hour and 30 minutes to the current time. You can do this quite easily with the DateTime
and DateInterval
objects. The following code adds 1 1/2 hour to the current time and outputs the hour(24) and minutes.
$date = new DateTime();
$date->add(new \DateInterval("PT1H30M")); // add 1,5 hour
$date->format("H:i");
If you want other formats take a look at the date formats
Solved it with help from Andreas :) //get the time one hour from now rounded off to nearest 5 minute date_default_timezone_set('Europe/Stockholm'); //Set timezone $startTime = strtotime(date("Y-m-d H:i:s"))+30*60; $NewMinute = round(date("i", $startTime)/5)*5; $num_padded = sprintf("%02d", $NewMinute); //When the NewMinute is less than 2 digits add leading zero $time_to_set = date("H:", $startTime) .$num_padded;