|
-
Jan 21st, 2005, 04:33 PM
#1
Thread Starter
Lively Member
Format PHP Date
I save dates to the database in the following format - "1979-12-28"
When displaying them on the website I want to display it as "28 Dec 1979" so I tried this:
echo "Born:" . date("d M Y",$myrow["DOB"]) . "<br>";
but that converts "1979-12-28" to "Born:31 Dec 1969"
What am I doing wrong?
-
Jan 21st, 2005, 04:48 PM
#2
Re: Format PHP Date
Are you using MySql? Databases often store dates in a different format. In MySql can convert the date in your database to a UNIX timestamp inside your query:
Code:
SELECT FROM table UNIX_TIMESTAMP(date) date;
Once this is done you can use it in the date function.
-
Jan 21st, 2005, 04:50 PM
#3
Thread Starter
Lively Member
Re: Format PHP Date
Thanks for your response. I'd rather keep it stored in my MYSQL database in date format for ease of use.
Isn't there anyway to convert "1979-12-28" to "28 Dec 1979"?
-
Jan 21st, 2005, 04:56 PM
#4
Re: Format PHP Date
Its still going to be stored in date format. You will be converting it to the format PHP understands inside the SELECT query, it won't change the format in which the data is stored in your database.
Post your query and I'll show you what I mean if you still don't follow.
-
Jan 21st, 2005, 04:57 PM
#5
Addicted Member
Re: Format PHP Date
Yes, the timestamp argument "$myrow["DOB"]" is in the wrong format.
string date ( string format [, int timestamp] )
Try:
echo "Born:" . date("d M Y",mktime(0, 0, 0, substr($myrow["DOB"],5,2), substr($myrow["DOB"],8,2), substr($myrow["DOB"],0,4))) . "<br>";
Edit: Actually, changing the query may be cleaner, but you'll need say an alias like UNIX_TIMESTAMP(Your_DOB_column) AS DOB_AsTimeStamp and use $myrow["DOB_AsTimeStamp"]
echo "Born:" . date("d M Y",$myrow["DOB_AsTimeStamp"]) . "<br>";
Last edited by Phenix; Jan 21st, 2005 at 05:08 PM.
Reason: Clarify PHP timestamp argument; Added "Actually" comment
Circa 1995
Engineer - I think we should put our website address on our paper catalogs.
Vice President - Don't get too excited about this internet thing.
I am sorry, but the Oracle was mistaken. You cannot help us.
-Matrix video game
I'm doing a (free) operating system (just a hobby, won't be big and professional like gnu) for 386(486) AT clones. ... and it probably never will support anything other than AT-harddisks, as that's all I have :-(.
-Linus
Question. Do you know that the character "?" means I'm asking a question? Question. Do you know that spoken inflection also provides the same cue? So please don't say, "Question" before you ask your question. Believe me I'll know.
That said, I would have said this first if it had to precede what I'm telling you now. Having said that, what I'm telling you now is the same thing I just said about the annoying phrases "That said" and "Having said that".
Are you threatening me, Master Jedi?
-Chancellor Palpatine
-
Jan 21st, 2005, 05:12 PM
#6
Thread Starter
Lively Member
Re: Format PHP Date
Thanks Phenix.
Used:
<b>Born: </b> <?php echo date("d M Y",mktime(0, 0, 0, substr($DOB,5,2), substr($DOB,8,2), substr($DOB,0,4))); ?> <br>
and it worked a treat.
Thanks for your help also visualAd
-
Jan 21st, 2005, 08:36 PM
#7
Fanatic Member
Re: Format PHP Date
You could try the strtotime() function.
It would look something like this:
PHP Code:
echo 'Born: ' . date('d M Y',strtotime($myrow['DOB'])) . '<br />';
Never argue with fools, they will only drag you down to their level, and beat you with experience.
Q: How do you tell an experienced hacker from a novice?
A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer
-
Jan 25th, 2005, 04:56 PM
#8
Re: Format PHP Date
 Originally Posted by JamesNZ
Thanks Phenix.
Used:
<b>Born: </b> <?php echo date("d M Y",mktime(0, 0, 0, substr($DOB,5,2), substr($DOB,8,2), substr($DOB,0,4))); ?> <br>
and it worked a treat.
Thanks for your help also visualAd 
James,
Most databases store dates in a different format, all these databases have the functions requied to convert form their native date types to unix timestamps.
Its all very well using the solution posted by Penix, but it will be both slower and will break should MySql one day decide to change thier date format or you need to use another database, it will break. So what at first, looks good and easy to use, in the long run it will probably give you a headache.
-
Jan 25th, 2005, 10:44 PM
#9
Thread Starter
Lively Member
Re: Format PHP Date
 Originally Posted by visualAd
James,
Most databases store dates in a different format, all these databases have the functions requied to convert form their native date types to unix timestamps.
Its all very well using the solution posted by Penix, but it will be both slower and will break should MySql one day decide to change thier date format or you need to use another database, it will break. So what at first, looks good and easy to use, in the long run it will probably give you a headache.
What solution would you suggest visualAd?
-
Jan 26th, 2005, 03:56 AM
#10
Fanatic Member
Re: Format PHP Date
I think this is what he is suggesting:
 Originally Posted by visualAd
Are you using MySql? Databases often store dates in a different format. In MySql can convert the date in your database to a UNIX timestamp inside your query:
Code:
SELECT FROM table UNIX_TIMESTAMP(date) date;
Once this is done you can use it in the date function.
Never argue with fools, they will only drag you down to their level, and beat you with experience.
Q: How do you tell an experienced hacker from a novice?
A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer
-
Jan 27th, 2005, 12:53 PM
#11
Re: Format PHP Date
 Originally Posted by JamesNZ
What solution would you suggest visualAd?
The one I posted which involves doing the conversion inside the SELECT query using the UNIX_TIMESTAMP() function. This will be quicker and more efficient than using PHP's string manipulation functions and make your code mroe portable.
-
Sep 23rd, 2007, 06:27 AM
#12
Addicted Member
Re: Format PHP Date
I need It, But from 2005 This problem not solved !!!!!!!!!!
How can I show a date in the format of 11/12/2007 from database?
-
Sep 23rd, 2007, 08:43 AM
#13
Re: Format PHP Date
Start your own thread for your own questions please.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|