Skip to content

Instantly share code, notes, and snippets.

@alexlatam
Created July 23, 2024 04:51
Show Gist options
  • Save alexlatam/58cb805868474115fa5b194f83b232c9 to your computer and use it in GitHub Desktop.
Save alexlatam/58cb805868474115fa5b194f83b232c9 to your computer and use it in GitHub Desktop.
PHP Dates
<?php
echo date("Y/m/d"); // Current date on format: 2024/12/24
echo date("Y-m-d"); // Current date on format: 2024-12-24
echo date("h:i:sa"); // Current hour on format: 04:33:39am
echo date("h:i:s"); // Current hour on format: 04:33:39
echo date("H:I:s"); // Current hour on format: 20:33:39
echo date('Y-m-d H:i:s'); // Current date: 2024-07-23 12:34:56
echo time(); // Current date on Epoch format: 1721709370
echo date("Y-m-d", time()); // Current date from Epoch format to other format: 2024-12-24
echo mktime(0, 0, 0, 7, 23, 2024); // Get Epoch date from specific date: 1721709370
echo strtotime('2024-07-23 12:34:56'); // Get Epoch date from specific string date: 1721709370
// DateTime class
$date = new DateTime('2024-07-23 12:34:56'); // create a new date object from DateTime class
$date = DateTime::createFromFormat('U', 1727079296); // create a new date object from DateTime class using Epoch format
$date = date_create('2024-07-23 12:34:56'); // alias of new DateTime()
$date->modify('+1 day'); // Add one day to date
echo $date->format('Y-m-d H:i:s'); // Get date on specific format: 2024-07-23 12:34:56
echo $date->getTimestamp(); // Get Epoch timestamp: 1727079296
// Timezones
date_default_timezone_set('America/New_York'); // set timezone for an specific file
echo date_default_timezone_get(); // Get the timezone: America/New_York
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment