Created
March 19, 2024 20:48
-
-
Save gabrielbarros/0ef4c1e4c63ab3cb7eca37a59bcd5150 to your computer and use it in GitHub Desktop.
Replace date, gmdate, time, mktime, gmmktime with DateTime class
This file contains 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 | |
// 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