Convert timestamp to date php

I have sầu a timestamp stored in a session (1299446702).

Bạn đang xem: Convert timestamp to date php

How can I convert that to lớn a readable date/time in PHP? I have tried srttotime, etc. lớn no avail.

You can go through khổng lồ for epoch or timestamp lớn human readable date using PHP.. – Laeeq Jun 17 "19 at 7:27

strtotime makes a date string inkhổng lồ a timestamp. You want to lớn vày the opposite, which is date. The typical mysql date format is date("Y-m-d H:i:s"); Cheông chồng the manual page for what other letters represent.

If you have sầu a timestamp that you want khổng lồ use (apparently you do), it is the second argument of date().

it would be good note that I passed in my current date as follows: emang đến date('m/d/Y', strtotime($current_date_variable); – cwiggo Aquảng bá 8 "15 at 11:26
is there no comm& to convert an timestamp to lớn their datetime standardisation becouse i converted an datetime to lớn an unix timestamp but in my database it is still an datetime i wil try this but i still find it weird that there is no specific comm& for it – Marc Roelse Jan 15 "trăng tròn at 14:46

I just added H:i:s to lớn Rocket"s answer lớn get the time along with the date.

Xem thêm: Php: Get Values Of Multiple Checked Checkboxes In Php 8, Handling Checkbox In A Php Form Processor

emang lại date("m/d/Y H:i:s", 1299446702);

Output: 03/06/2011 16:25:02

$timestamp = 1465298940; $datetimeFormat = "Y-m-d H:i:s"; $date = new DateTime(); // If you must have use time zones // $date = new DateTime("now", new DateTimeZone("Europe/Helsinki")); $date->setTimestamp($timestamp); emang lại $date->format($datetimeFormat);

result: 2016-06-07 14:29:00

Other time zones:

Africa America Antarctica Arctic Asia Atlantic nước Australia Europe Indian Pacific Others

If you are using PHP.. date(), you can use this code to get the date, time, second, etc.

$time = time(); // you have sầu 1299446702 in time $year = $time/31556926 % 12; // lớn get year $week = $time / 604800 % 52; // to lớn get weeks $hour = $time / 3600 % 24; // to get hours $minute = $time / 60 % 60; // khổng lồ get minutes $second = $time % 60; // khổng lồ get seconds
How about leap year & second...? – Petteri H Jan 26 "15 at 9:22
$year = date('Y', $time) – Ruben Kazumov Oct 29 "16 at 14:26

I know that"s an old one question, but its high in the search results.

If anyone wants timestamp conversion directly to lớn a DateTime object, there"s a simple one-liner:

$timestamp = 1299446702; $date = DateTime::createFromFormat("U", $timestamp);

sromero phản hồi, timezone parameter (the 3rd param in DateTime::createFromFormat()) is ignored when unix timestamp is passed, so the below code is unnecessary.

$date = DateTime::createFromFormat("U", $timestamp, new DateTimeZone("UTC"); // not needed, 3rd parameter is ignored

You may kiểm tra PHP"s manual for DateTime::createFromFormat for more info và options.

Jus a phản hồi about your last line of code: according lớn the manual page you link: "Note: The timezone parameter and the current timezone are ignored when the time parameter either contains a UNIX timestamp (e.g. 946684800) or specifies a timezone (e.g. 2010-01-28T15:00:00+02:00)." – sromero Nov 1 "18 at 12:39
sromero You have sầu right. The timezone parameter is ignored in this case. – Pascal Nov 2 "18 at 9:22
Hi there! It seems that this answer has already been proposed; see the top answer. – hat Mar 6 "19 at 10:25

You can try this:

$mytimestamp = 1465298940; echo gmdate("m-d-Y", $mytimestamp);

Output :


Unless you need a custom date and time format, it"s easier, less error-prone, và more readable lớn use one of the built-in date time format constants:

emang đến date(DATE_RFC822, 1368496604);
This should be the top answer. – alexg Jan 31 "19 at 11:43

$epoch"); // convert UNIX timestamp lớn PHP DateTime echo $dt->format("Y-m-d H:i:s"); // output = 2017-01-01 00:00:00

In the examples above "r" và "Y-m-d H:i:s" are PHP date formats, other examples:

Format Output

r ----- Wed, 15 Mar 2017 12:00:00 +0100 (RFC 2822 date) c ----- 2017-03-15T12:00:00+01:00 (ISO 8601 date) M/d/Y ----- Mar/15/2017 d-m-Y ----- 15-03-2017 Y-m-d H:i:s ----- 2017-03-15 12:00:00

emang lại date("l M j, Y",$res1<"timep">); This is really good for converting a unix timestamp khổng lồ a readable date along with day. Example: Thursday Jul 7, 2016

Similar answers are already posted, what is the plus of information in your answer? – Steffen Harbich Jul 7 "16 at 11:27
Highly active question. Earn 10 reputation in order to lớn answer this question. The reputation requirement helps protect this question from spam & non-answer activity.

Not the answer you're looking for? Browse other questions tagged php or ask your own question.

Proofs of theorems that proved more or deeper results than what was first supposed or stated as the corresponding theorem

How could a lost time traveller quickly và quietly determine they've arrived in 500 BC France?
Would a man looking at his own wife 'to lớn desire her' be committing adultery according khổng lồ Jesus at Matthew 5:28?

Why vì atoms arrange themselves in a regular fashion to form crystals? more hot questions
Stack Overflow Questions Jobs Developer Jobs Directory Salary Calculator Help điện thoại
Products Teams Talent Advertising Enterprise
Company About Press Work Here Legal Privacy Policy Terms of Service Contact Us
Stack Exchange Network Technology Life / Arts Culture / Recreation Science Other
Staông xã Overflow Server Fault Super User Web Applications Ask Ubuntu Webmasters trò chơi Development
TeX - LaTeX Software Engineering Unix & Linux Ask Different (Apple) WordPress Development Geographic Information Systems Electrical Engineering
Android Enthusiasts Information Security Database Administrators Drupal Answers SharePoint User Experience Mathematica
Salesforce ExpressionEngine® Answers Staông chồng Overflow em Português Blender Network Engineering Cryptography Code nhận xét
Magenkhổng lồ Software Recommendations Signal Processing Emacs Raspberry Pi Stack Overflow на русском Code Golf
Stack Overflow en español Ethereum Data Science Arduino Bitcoin Software Quality Assurance & Testing Sound Design
Windows Phone more (28)
Photography Science Fiction & Fantasy Graphic Design Movies & TV Music: Practice & Theory Worldbuilding Video Production
Seasoned Advice (cooking) trang chủ Improvement Personal Finance & Money Academia Law Physical Fitness Gardening & Landscaping
Parenting more (10)
English Language & Usage Skeptics Mi Yodeya (Judaism) Travel Christianity English Language Learners Japanese Language
Chinese Language French Language German Language Biblical Hermeneutics History Spanish Language Islam
Русский язык Russian Language Arqade (gaming) Bicycles Role-playing Games Anime & Manga Puzzling
Motor Vehicle Maintenance & Repair Board & Card Games Bricks Homebrewing Martial Arts The Great Outdoors Poker
Chess Sports more (16)
MathOverflow Mathematics Cross Validated (stats) Theoretical Computer Science Physics Chemistry Biology
Computer Science Philosophy Linguistics Psychology & Neuroscience Computational Science more (10)

Blog Facebook Twitter LinkedIn Instagram

site design / hình ảnh sản phẩm © 2021 Staông chồng Exchange Inc; user contributions licensed under cc by-sa. rev 2021.3.5.38726