datetime - integrating a round up to my php time code
:-) So I do have edited some code I found online to this:
function timeZone_funch( $atts )
{
extract(shortcode_atts(array('timezone' => 'Europe/Berlin'), $atts));
/* Europe/Berlin is default Timezone */
$output = '';
if (in_array($timezone, DateTimeZone::listIdentifiers()))
{
date_default_timezone_set($timezone);
$currentTime = date( 'H:i', strtotime('+30 minutes'));
$output = $currentTime;
}
else {
$output = "Invalid Timezone";
}
return $output;
}
add_shortcode( 'current_time', 'timeZone_funch' );
It works as it should. It displays the timezones hour and minute + adding 30 minutes.
What I want to do now is to make it round up to the nearest quater hour.
Similar to this thread: Round minute down to nearest quarter hour
I also believe that this thread contains my answer BUT due to my lack of knowledge and experience, I'm not able to work the code from the thread above into the code I have right now.
If anybody could show me how I can integrate it, this would be amazing!
Thank you for your time and maybe even your answer!
With kind regards Chris
Finding the correct way to integrate the round up function inside of the code I'am using.
Answer
Solution:
<?php
function timeZone_funch( $atts, $timezone )
{
// extract(shortcode_atts(array('timezone' => 'Europe/Berlin'), $atts));
/* Europe/Berlin is default Timezone */
$output = '';
if (in_array($timezone, DateTimeZone::listIdentifiers()))
{
date_default_timezone_set($timezone);
// Get the time after 30 minutes and round to the closest quarter
$seconds = strtotime('+30 minutes');
$roundedSeconds = round($seconds / (15 * 60)) * (15 * 60);
$currentTime = date( 'H:i', $roundedSeconds );
$output = $currentTime;
}
else {
$output = "Invalid Timezone";
}
return $output;
}
// add_shortcode( 'current_time', 'timeZone_funch' );
echo timeZone_funch(array(), 'Europe/Berlin');
Answer
Solution:
Here is the solution that worked! Thanks again to D.Dimopoulos! :-)
function timeZone_funch( $atts )
{
extract(shortcode_atts(array('timezone' => 'Europe/Berlin'), $atts));
/* Europe/Berlin is default Timezone */
$output = '';
if (in_array($timezone, DateTimeZone::listIdentifiers()))
{
date_default_timezone_set($timezone);
$seconds = strtotime('+30 minutes');
$roundedSeconds = round($seconds / (15 * 60)) * (15 * 60);
$currentTime = date( 'H:i', $roundedSeconds );
$output = $currentTime;
}
else {
$output = "Invalid Timezone";
}
return $output;
}
add_shortcode( 'current_time', 'timeZone_funch' );
Source