Handy PHP
Forum
Welcome, Guest
Please Login or Register.    Lost Password?
Reformat Date
(1 viewing) 1 Guest
Go to bottom
TOPIC: Reformat Date
#6
vujsa
Moderator
Posts: 143
graph
User Offline Click here to see the profile of this user
Reformat Date 3 Years, 10 Months ago Karma: 5
This topic is for the discussion of the Handy PHP function Reformat Date.

Please keep the discussion related to this function.
 
Logged Logged
 
Do not post small or one line posts.
Do not post unsolicited links or any referal links.
Do not post copied content.
Do not use foul language.
Always be respectful to the other posters.
  The administrator has disabled public write access.
#341
jej1216
Fresh Boarder
Posts: 1
graphgraph
User Offline Click here to see the profile of this user
Re:Reformat Date 1 Year, 10 Months ago Karma: 0
I have a date field in a PHP form, and it uses Javascript to provide a pop-up calendar to select the date (user can also enter the date by typing). The format is yyyy-MM-dd.

The form posts to a MySQL DB, which accepts this date format.

The users want the form to show the dates as MM/dd/yyyy.

I can change the Javascript to display this format, but I need to pass the value in the yyyy-MM-dd format. I then also have to dieplay the date back in the MM/dd/yyyy format in another PHP page that the user can use to update the data.

I'm assuming that I have to do this reformatting in the PHP pages - but what do I code? I assume I need to use strtotime().

Do I write a function for this?

TIA,

jej1216
 
Logged Logged
  The administrator has disabled public write access.
#342
vujsa
Moderator
Posts: 143
graph
User Offline Click here to see the profile of this user
Re:Reformat Date 1 Year, 10 Months ago Karma: 5
Since you already have your date stored in your database in the standard format, you can use a MySQL date function to prepare your data before it sends it to your PHP script.

MySQL DATE FORMAT Function

Otherwise, you can you the function I wrote to do the job. You can find it here: Reformat Date

It is a quick and easy bit of code that utilizes the strtotime()function in conjunction with the date() function to convert date formats without a lot of extra coding.

As for your JavaScript dates, I don't really do much about JS here but I can go into it if you like...

As for creating a function for your application, it might be a good idea...
If you created a function like the one I did, you could always pass all of your dates through the function to ensure that your website is uniform. You could even set your date format as a variable that you could set in your configuration section of your script which you could change later if you desired to. But that gets a bit into the content management realm of programming.

Hopes this helps,
vujsa
 
Logged Logged
 
Do not post small or one line posts.
Do not post unsolicited links or any referal links.
Do not post copied content.
Do not use foul language.
Always be respectful to the other posters.
  The administrator has disabled public write access.
Go to top
Earn More with LinkConnector