Hướng dẫn php get current datetime

For the new PHP programmer might confuse why there are lot of method for to get current date and time and which one to use in their project.

1. date method [PHP 4, PHP 5, PHP 7]

This is the very common and very easiest way to get the date and time in php.

// set the default timezone to use. Available since PHP 5.1
date_default_timezone_set['UTC'];


// Prints something like: Monday
echo date["l"];

// Prints something like: Monday 8th of August 2005 03:12:46 PM
echo date['l jS \of F Y h:i:s A'];

// Prints: July 1, 2000 is on a Saturday
echo "July 1, 2000 is on a " . date["l", mktime[0, 0, 0, 7, 1, 2000]];

/* use the constants in the format parameter */
// prints something like: Wed, 25 Sep 2013 15:28:57 -0700
echo date[DATE_RFC2822];

// prints something like: 2000-07-01T00:00:00+00:00
echo date[DATE_ATOM, mktime[0, 0, 0, 7, 1, 2000]];

You can learn more about it in here

2. DateTime class [PHP 5 >= 5.2.0, PHP 7]

when you want to use PHP with OOP, this is the best way to get date and time.


You can learn more about it in here

3. Carbon Date time package

if you are using Composer, Laravel, Symfony or any kinda framework this is the best way to get the date and time. Also this package extends DateTime class in php so you use all the method in Datetime class. This in-built in frameworks like laravel so you don't have to install it separately.

printf["Right now is %s", Carbon::now[]->toDateTimeString[]];
printf["Right now in Vancouver is %s", Carbon::now['America/Vancouver']]; // automatically converted to string
$tomorrow = Carbon::now[]->addDay[];
$lastWeek = Carbon::now[]->subWeek[];

// Carbon embed 823 languages:
echo $tomorrow->locale['fr']->isoFormat['dddd, MMMM Do YYYY, h:mm'];
echo $tomorrow->locale['ar']->isoFormat['dddd, MMMM Do YYYY, h:mm'];

$officialDate = Carbon::now[]->toRfc2822String[];

$howOldAmI = Carbon::createFromDate[1975, 5, 21]->age;

$noonTodayLondonTime = Carbon::createFromTime[12, 0, 0, 'Europe/London'];

$internetWillBlowUpOn = Carbon::create[2038, 01, 19, 3, 14, 7, 'GMT'];

if [Carbon::now[]->isWeekend[]] {
    echo 'Party!';
}
echo Carbon::now[]->subMinutes[2]->diffForHumans[]; // '2 minutes ago'

You can learn more about it in here

Hope this helps and if you know any other way to get the date and time feel free to edit the answer.

Which PHP function can return the current date/time?

asked Jan 22, 2009 at 20:09

2

Your Country Time Zone: List of Supported Timezones

date_default_timezone_set['Asia/Kolkata'];

$dateYmd = date['Y-m-d'];
echo "Current Year Month Day: $dateYmd";

Current Year Month Day: 2022-01-03

$datehms = date['h:i:s'];
echo "Current Hour Minute Second: $datehms";

Current Hour Minute Second: 11:05:38

droidlabour

4672 gold badges6 silver badges21 bronze badges

answered Jan 3 at 17:06

Mr. CoderxMr. Coderx

4216 silver badges4 bronze badges

// Set the default timezone to use. Available since PHP 5.1
date_default_timezone_set['UTC'];


// Prints something like: Monday
echo date["l"];

// Prints something like: Monday 8th of August 2016 03:12:46 PM
echo date['l jS \of F Y h:i:s A'];

// Prints: July 1, 2016 is on a Saturday
echo "July 1, 2016 is on a " . date["l", mktime[0, 0, 0, 7, 1, 2016]];

/* Use the constants in the format parameter */
// Prints something like: Wed, 25 Sep 2013 15:28:57 -0700
echo date[DATE_RFC2822];

// Prints something like: 2016-07-01T00:00:00+00:00
echo date[DATE_ATOM, mktime[0, 0, 0, 7, 1, 2000]];

answered Aug 19, 2015 at 23:57

ElyorElyor

5,0906 gold badges44 silver badges73 bronze badges

Here are some characters that are commonly used for times:

  1. h - 12-hour format of an hour with leading zeros [01 to 12]
  2. i - Minutes with leading zeros [00 to 59]
  3. s - Seconds with leading zeros [00 to 59]
  4. a - Lowercase Ante meridiem and Post meridiem [am or pm]

Get your time zone


Check this out [optional]


For date


Here are some characters that are commonly used for dates:

  1. d - Represents the day of the month [01 to 31]
  2. m - Represents a month [01 to 12]
  3. Y - Represents a year [in four digits]
  4. l [lowercase 'L'] - Represents the day of the week

Source-W3-Schools

answered Aug 29, 2017 at 13:24

Raheel KhanRaheel Khan

40410 silver badges12 bronze badges

2

simply use: date["Y-m-d H:i:s"] this will give you your date and time like '2020-08-22 12:20:30' this . add date_default_timezone_set["your time zone"] before date[] function to get the time date of your area/zone. here you can find you time zone

answered Aug 24, 2020 at 6:25

Shiba DasShiba Das

3304 silver badges12 bronze badges

2

Here are some characters that are commonly used for times:

  • d - Represents the day of the month [01 to 31]

  • m - Represents a month [01 to 12]

  • Y - Represents a year [in four digits]

  • l [lowercase 'L'] - Represents the day of the week

  • H - 24-hour format of an hour [00 to 23]

  • h - 12-hour format of an hour with leading zeros [01 to 12]

  • i - Minutes with leading zeros [00 to 59]

  • s - Seconds with leading zeros [00 to 59]

  • a - Lowercase Ante meridiem and Post meridiem [am or pm]

example :

echo "Today " . date["Y/m/d"] ;
echo "time " . date["h:i:sa"];

answered Nov 19, 2020 at 13:42

Tijo JohnTijo John

6761 gold badge9 silver badges20 bronze badges

If you want to get the date like 12-3-2016, separate each day, month, and year value, then copy-paste this code:

$day = date["d"];
$month = date["m"];
$year = date["y"];
print "date" . $day . "-" . $month . "-" . $year;

answered Mar 12, 2016 at 10:35

Pir Fahim ShahPir Fahim Shah

10k1 gold badge77 silver badges78 bronze badges

We can use the date function and set the default timezone:


answered Dec 27, 2017 at 11:32

PHP returns the current time in seconds. You need to format them in whatever format you want.


Reference: //php.net/manual/en/function.strftime.php

answered Dec 9, 2016 at 20:24

Gnanasekar SGnanasekar S

1,72013 silver badges14 bronze badges

You can simply use this code to get current date and time

echo date['r', time[]];

answered Nov 14, 2019 at 7:50

Karthik SWOTKarthik SWOT

8891 gold badge10 silver badges15 bronze badges

Linux server time and PHP time[] difference time zone as follows:


answered Jun 13, 2018 at 9:20

Chủ Đề