How to get the current date and time in php

The time would go by your server time. An easy workaround for this is khổng lồ manually set the timezone by using date_default_timezone_phối before the date() or time() functions are called to lớn.

Bạn đang xem: How to get the current date and time in php

I"m in Melbourne, Australia so I have something lượt thích this:

date_default_timezone_set("Australia/Melbourne");Or another example is LA - US:

date_default_timezone_set("America/Los_Angeles");You can also see what timezone the server is currently in via:

date_default_timezone_get();So something like:

$timezone = date_default_timezone_get();echo "The current server timezone is: " . $timezone;So the short answer for your question would be:

// Change the line below lớn your timezone!date_default_timezone_set("Australia/Melbourne");$date = date("m/d/Y h:i:s a", time());Then all the times would be khổng lồ the timezone you just mix :)


Share
Improve sầu this answer
Follow
edited Aug 15 "14 at 21:06
*

Steve Chambers
31.3k1515 gold badges127127 silver badges172172 bronze badges
answered Jan 23 "09 at 0:19
*

NJ.NJ.
6,79911 gold badge1515 silver badges1919 bronze badges
8
| Show 3 more comments
556

Share
Improve sầu this answer
Follow
edited Jul 5 "18 at 7:46
*

bluish
22.8k2323 gold badges110110 silver badges167167 bronze badges
answered Mar 6 "12 at 14:42
*

Shadrachồng B. OrinaShadrack B. Orimãng cầu
6,85522 gold badges2727 silver badges3535 bronze badges
2
Add a comment |
215
Since PHPhường 5.2.0 you can use the DateTime() class:

use Datetime;$now = new DateTime();eđến $now->format("Y-m-d H:i:s"); // MySquốc lộ datetime formatemang lại $now->getTimestamp(); // Unix Timestamp -- Since PHPhường 5.3And khổng lồ specify the timezone:

$now = new DateTime(null, new DateTimeZone("America/New_York"));$now->setTimezone(new DateTimeZone("Europe/London")); // Another wayemang lại $now->getTimezone();
Share
Improve this answer
Follow
edited Mar 16 "20 at 8:35
answered May 29 "13 at 12:44

*

MahdiMahdi
8,30377 gold badges4949 silver badges6666 bronze badges
2
Add a phản hồi |
102
Reference: Here"s a link

This can be more reliable than simply adding or subtracting the number of seconds in a day or a month to lớn a timestamp because of daylight saving time.

The PHPhường. code

Share
Improve this answer
Follow
edited Sep 24 "16 at 10:44
webnoob
14.6k1111 gold badges7171 silver badges149149 bronze badges
answered Aug 29 "13 at 18:49
ShapCyberShapCyber
2,63611 gold badge1717 silver badges2323 bronze badges
1
Add a bình luận |
71
PHP"s time() returns a current Unix timestamp. With this, you can use the date() function khổng lồ format it to lớn your needs.

$date = date("Format String", time());As Paolo mentioned in the comments, the second argument is redundant. The following snippet is equivalent lớn the one above:

$date = date("Format String");
Share
Improve this answer
Follow
edited Feb 22 "19 at 23:38
Peter Mortensen
27.6k2121 gold badges9494 silver badges123123 bronze badges
answered Jan 22 "09 at 20:13
Rob HruskaRob Hruska
110k2727 gold badges160160 silver badges186186 bronze badges
1
Add a bình luận |
69
You can either use the $_SERVER<"REQUEST_TIME"> variable (available since PHP.. 5.1.0) or the time() function to lớn get the current Unix timestamp.


Share
Improve sầu this answer
Follow
answered Jan 22 "09 at 20:11
GumboGumbo
586k100100 gold badges734734 silver badges811811 bronze badges
2
Add a comment |
58
You can use both the $_SERVER<"REQUEST_TIME"> variable or the time() function. Both of these return a Unix timestamp.

Most of the time these two solutions will yield the exact same Unix Timestamp. The difference between these is that $_SERVER<"REQUEST_TIME"> returns the time stamp of the most recent server request và time() returns the current time. This may create minor differences in accuracy depending on your application, but for most cases both of these solutions should suffice.

Based on your example code above, you are going to lớn want to format this information once you obtain the Unix Timestamp. Unformatted Unix time looks like: 1232659628

So in order khổng lồ get something that will work, you can use the date() function khổng lồ format it.

A good reference for ways lớn use the date() function is located in the PHP.. Manual.

Xem thêm: Official Geforce Drivers Geforce Game Ready Driver Whql, Official Geforce Drivers

emang lại date("m/d/Y h:i:s a", time());
Share
Improve sầu this answer
Follow
edited Nov 11 "18 at 11:39
ashleedawg
1
answered Jan 22 "09 at 21:36
Grizzly Peak SoftwareGrizzly Peak Software
1,26644 gold badges1414 silver badges2020 bronze badges
2
Add a bình luận |
45
PHP"s date function can vị this job.

date()

Description:

string date(string $format <, int $timestamp = time()>)Returns a string formatted according khổng lồ the given format string using the given integer timestamp or the current time if no timestamp is given.

Examples:

Share
Improve this answer
Follow
edited Feb 22 "19 at 23:44
Peter Mortensen
27.6k2121 gold badges9494 silver badges123123 bronze badges
answered Apr 17 "15 at 11:43
Subodh GhulaxeSubodh Ghulaxe
17.1k1414 gold badges7777 silver badges9595 bronze badges
Add a comment |
30
For the new PHPhường. programmer might confuse why there are lot of method for lớn get current date và time và which one khổng lồ use in their project.

1. date method (PHP 4, PHP 5, PHPhường. 7)

This is the very comtháng và very easiest way to get the date and time in php.

2. DateTime class (PHP 5 >= 5.2.0, PHP.. 7)

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

3. Carbon Date time package

if you are using Composer, Laravel, Symfony or any kindomain authority framework this is the best way khổng lồ 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 sầu to lớn install it separately.

Xem thêm: sửa lỗi wmvcore.dll

printf("Right now is %s", Carbon::now()->toDateTimeString());printf("Right now in Vancouver is %s", Carbon::now("America/Vancouver")); // automatically converted khổng lồ string$tomorrow = Carbon::now()->addDay();$lastWeek = Carbon::now()->subWeek();// Carbon embed 823 languages:emang đến $tomorrow->locale("fr")->isoFormat("dddd, MMMM Do YYYY, h:mm");emang lại $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()) eđến "Party!";emang lại Carbon::now()->subMinutes(2)->diffForHumans(); // "2 minutes ago"You can learn more about it in here

Hope this helps và if you know any other way lớn get the date & time feel free to lớn edit the answer.


Chuyên mục: