Last active
March 5, 2017 11:11
-
-
Save kerstvo/10110220 to your computer and use it in GitHub Desktop.
PHP: get next hour
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Thanks to: http://www.sitepoint.com/forums/showthread.php?708194-Is-it-possible-to-round-up-date()-or-strtotime()-to-nearest-hour&s=086875bf39a87d999fd9db47ac69bcf0&p=4720952&viewfull=1#post4720952 | |
// php >= 5.3 | |
$date = '2014-04-01 18:30:00'; | |
$datetime = DateTime::createFromFormat('Y-m-d H:??:??', $date); | |
$start = $datetime->format('g A'); | |
$end = $datetime->modify('next hour')->format('g A'); | |
echo "Scheduled between $start and $end."; | |
// php <= 5.2 | |
$date = '2014-04-01 18:30:00'; | |
$start = date('g A', strtotime($date)); | |
$end = date('g A', strtotime('+1 hour', strtotime($date))); | |
echo "Scheduled between $start and $end."; | |
// php = 5.2 | |
// alternative | |
$date = '2014-04-01 18:30:00'; | |
$datetime = new DateTime($date); | |
$start = $datetime->format('g A'); | |
$datetime->modify('+1 hour'); | |
$end = $datetime->format('g A'); | |
echo "Scheduled between $start and $end."; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment