This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Numerical Date To Text Date PHP
I'm trying to find a way to convert a date to a more "user-friendly" format.
For example, from 2011-01-05 to Jan 5th, 2011.
I'm sure it's something simple, I just can't seem to find it anywhere. Any ideas?
For the th add the English ordinal Suffix for the day of the month
date("M jS, Y", strtotime("2011-01-05"));
Try this:
date("M j, Y", strtotime("2011-01-05"));
You could use the DateTime class, and call the format function. Then just set up the format using the standard date() format string to output a textual data format.
Related
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 2 years ago.
In a database, the date is stored like this: 14 May 2020 22:25
I was wondering, when reading this line from database, and change the output to this: 14-5-2020, is it possible to convert this easily? because sometimes i want to output 14 May 2020 22:25 and sometimes i want to output 14-5-2020. And if is possible, i only want to store 1 format in the database
<?php
$date = DateTime::createFromFormat('d M Y H:i', '14 May 2020 22:25');
echo $date->format('d-n-Y'); //14-5-2020
As mentioned in the comment by #Sammitch, you can use PHP DateTime Class to achieve this.
You can format the Date and Time, once you create it. Be sure to check the format guide. Given below as link.
Useful Links:
Formatting DateTime in PHP
PHP DateTime
This question already has answers here:
PHP-MYSQL: Converting Unix Timestamp to DateTime and vice versa
(5 answers)
Closed 6 years ago.
Im currently struggeling with timestamps. I have Googled for a simple answer for this, but cant find any solution for my problem. I want to convert user input into UNIX time. The user inputs for example "26/03/1982", how do I convert this input into UNIX timestamp?
//User input
$birthday = "26/03/1982"; // d/m/y
$birthday = [CONVERTED TO UNIX]
Short and simple question. I will add more if you'd like. Thanks.
Look into the PHP function strtotime:
strtotime — Parse about any English textual datetime description into a Unix timestamp
I'm not sure how well it handles / in dates, but replace them with - and it'll work.
Example
echo strtotime(str_replace("/","-","26/03/1982"));
outputs 385977600 which is the Unix date for Fri, 26 Mar 1982
You may need to set your date_default_timezone for this to work
This question already has answers here:
How to create human readable time stamp?
(6 answers)
Closed 6 years ago.
i'm currently looking at something and it says this.
"last_updated":1471323637
how can i convert that number into a time that I can actually read because at the moment i have no idea what time/date that is.
thank you.
Use FROM_UNIXTIME if you want to convert it to human readable time in MySQL
SELECT FROM_UNIXTIME(1471323637);
And you will get an output like below:
2016-08-16 11:00:37
Try this
echo date('H:i:s d/m/y' ,1471323637);
Output
10:30:37 16/08/16
for more info about date please read http://php.net/manual/en/function.date.php
You can also try this
echo date("g:i a, j M Y" ,1471323637);
Output
10:30 am, 16 Aug 2016
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 9 years ago.
I have a datetime field which saves the date in numerical format:
2013-11-23 21:21:31
I want to format this value into Y-m-d H:i:s so that instead of a list of numbers it reads as:
23rd November 2013
with or without the time, doesn't really matter too much.
Is this possible using date() or is that just for submitting data?
Convert the date string to a timestamp using strtotime() and feed it to date():
echo date('jS F Y', strtotime($dateStringFromDB));
For other available formatting options, refer to the documentation for date().
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
convert php date to mysql format
I have a date field which lets users type in their birthday in m/d/Y format (for example 06/01/1982) but just in case I also made my validation accept month and day values without any leading 0 (6/1/1982).
How can I convert these dates to Y-m-d for use in MySQL?
Other valid dates are 6/01/1982 and 06/1/1982.
You could use date() and strtotime() together like such:
$date = date('Y-m-d', strtotime('06/01/1982'));
That will give you
1982-06-01
I would validate all input using strtotime (which will handle all major date formats).
On the insert, cast it to the proper format using the inverse: strftime:
$userValidatedDate = strtotime($_GET['date']);
//...
$sqlDate = strftime('%Y-%m-%d',$userValidatedDate);