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
format date
Moderator: General Moderators
Re: format date
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
Re: format date
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
Thanks in advance for any assistance
<?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
Thanks in advance for any assistance
Re: format date
Try something like this:
Code: Select all
$newDate = date("M d, Y", strtotime($row_Cal_list['startdate']));
echo $newDate;
Re: format date
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.
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.
Re: format date
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
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