Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gabrielbarros/0ef4c1e4c63ab3cb7eca37a59bcd5150 to your computer and use it in GitHub Desktop.
Save gabrielbarros/0ef4c1e4c63ab3cb7eca37a59bcd5150 to your computer and use it in GitHub Desktop.
Replace date, gmdate, time, mktime, gmmktime with DateTime class
<?php
// date
echo "date\n";
echo date('Y-m-d H:i:s'), "\n";
$dt = new DateTime();
echo $dt->format('Y-m-d H:i:s'), "\n\n";
// gmdate (UTC date)
echo "gmdate\n";
echo gmdate('Y-m-d H:i:s'), "\n";
$dt = new DateTime('now', new DateTimeZone('UTC'));
echo $dt->format('Y-m-d H:i:s'), "\n\n";
// time()
echo "time\n";
echo time(), "\n";
$dt = new DateTime();
echo $dt->getTimestamp(), "\n\n"; // or $dt->format('U')
// mktime()
echo "mktime\n";
$hour = 12;
$minute = 0;
$second = 0;
$year = 2020;
$month = 4;
$day = 1;
echo mktime($hour, $minute, $second, $month, $day, $year), "\n";
$dt = new DateTime();
$dt->setTime($hour, $minute, $second);
$dt->setDate($year, $month, $day);
echo $dt->getTimestamp(), "\n\n";
// gmmktime()
echo "gmmktime\n";
echo gmmktime($hour, $minute, $second, $month, $day, $year), "\n";
$dt = new DateTime('now', new DateTimeZone('UTC'));
$dt->setTime($hour, $minute, $second);
$dt->setDate($year, $month, $day);
echo $dt->getTimestamp(), "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment