Skip to content

Instantly share code, notes, and snippets.

@kerstvo
Last active March 5, 2017 11:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kerstvo/10110220 to your computer and use it in GitHub Desktop.
Save kerstvo/10110220 to your computer and use it in GitHub Desktop.
PHP: get next hour
<?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