format date

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Alpal
Forum Commoner
Posts: 39
Joined: Mon Jul 26, 2010 4:08 am

format date

Post by Alpal »

Bet this has been asked a million times, so, this is 1,000,001
Have been looking around for the last 2 hours, maybe not in the right place
Searched this forum and lots of other places, saw all sorts of solutions, but simply could not understand how to do this

I want to format a date dd/mm/yyyy when returning it from my database, it appears there are many ways to do this, what is the best way

<?php echo $row_Cal_list['startdate']; ?>
returns yyyy/mm/dd

Is there a simple way to format the date?

Thanks in advance for any assistance
fugix
Forum Contributor
Posts: 207
Joined: Fri Mar 18, 2011 8:01 pm

Re: format date

Post by fugix »

try http://php.about.com/od/learnphp/ss/php_functions_3.htm...use the date() function and format it whichever way you want by using certain letters
Alpal
Forum Commoner
Posts: 39
Joined: Mon Jul 26, 2010 4:08 am

Re: format date

Post by Alpal »

OK, had a look at the site you sent me to. I understand how to format the date and would like to format it so

<?php echo date("M d, Y"); ?>

What I have currently is

<?php echo $row_Cal_list['startdate']; ?>

How do I put it together?

Sorry, but I tried all sorts of things and could not make it work
Maybe I should look for something else to do in my spare time :banghead:
Thanks in advance for any assistance
eivind
Forum Commoner
Posts: 28
Joined: Tue Apr 19, 2011 7:57 am

Re: format date

Post by eivind »

Try something like this:

Code: Select all

$newDate = date("M d, Y", strtotime($row_Cal_list['startdate']));
echo $newDate;
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: format date

Post by pickle »

What is stored in your database? Is it a UNIX timestamp (ie: 1303402055), is it a MySQL date stamp (ie 2011-04-20), or is it just a plain string (ie: 2011/04/20)?

If the latter (which I believe it is judging by what you've said), there are 2 ways to do it. One, you could explode() the string to retrieve the year, month and date as separate strings, then concatenate them together in a different order. The other way you could do it is by using strtotime() (which converts many different date strings into a UNIX timestamp), and date(), which allows you to generate date strings from a UNIX timestamp.

My suggestion would be to use strtotime() and date(), as it will allow some flexibility if you choose to change the structure of your date string in the database. The only warning is to make sure strtotime() can properly interpret your string.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Alpal
Forum Commoner
Posts: 39
Joined: Mon Jul 26, 2010 4:08 am

Re: format date

Post by Alpal »

Thankyou, works well
One small problem, if the field was empty it was returning start of time date, so I did this

<?php if ($row_Cal_list['startdate'] != ''): ?>
<?php $newDate = date("M d, Y", strtotime($row_Cal_list['startdate'])); echo $newDate; ?>
<?php endif; ?>

Not sure if this is the right way to do this, but it works

I will have to learn more about php, can you suggest a book ?
If you are ever down under, in Melbourne Australia, I would like to buy you a beer!
Your assistance is greatly appreciated
Post Reply