Page 1 of 1
diplaying date
Posted: Sun Nov 07, 2010 4:05 am
by Nomistake
Hi,
A small question
How can i display
as a date format. (yyyy/mm/dd)
i tried something like this:
but this resulst in something like 1970/08/21,
which isnt the date set to te variable $day (2010/11/05)
Someone can helpe with this?
Thanks!

Re: diplaying date
Posted: Sun Nov 07, 2010 11:28 am
by Jonah Bron
What you did is correct. The timestamp is apparently off. I tested that number, and I got the same results.
Re: diplaying date
Posted: Sun Nov 07, 2010 11:40 am
by s992
The date function only accepts Unix timestamps. I ran this to get the actual Unix timestamp:
Code: Select all
<?php
$day = "20101105";
$day = strtotime($day);
echo $day; // returns 1288915200
?>
Re: diplaying date
Posted: Sun Nov 07, 2010 12:11 pm
by Jonah Bron
Ooooohhhhh... it's not a timestamp.
Two methods. One processes the text, and the other uses built in methods to parse and format the date. The second one would be easier to understand quickly when reading code.
Method 1:
(like s992 said, you use strtotime() to parse it)
Code: Select all
$day = substr($day, 0, 4) . '/' . substr($day, 3, 2) . '/' . substr($day, 5, 2);
// $day = 2010/11/05
Method 2:
Code: Select all
$day = date('Y/m/d', strtotime($day));
Re: diplaying date
Posted: Sun Nov 07, 2010 12:18 pm
by jojovem
mktime() wont help you?
Re: diplaying date
Posted: Sun Nov 07, 2010 12:21 pm
by Jonah Bron
jojovem wrote:mktime() wont help you?
Yes, that method could be used... but in this case the date is together in a string. It would have to be split up anyway.
Re: diplaying date
Posted: Mon Nov 08, 2010 8:35 am
by Nomistake
Hi!
thanks for the replies.
I did it this way:
Code: Select all
$var1 = str_split($day,4);
$var2 = str_split($var1[1],2);
echo "date is ".$var1[0]."/".$var2[0]."/".$var2 [1];
maybe its's a bit overkill this way?
(it's possible i made a mistake in the code i typed here, i dont have my resource at hand)
i'll also will have a closer look at the solutions you provided...

Re: diplaying date
Posted: Mon Nov 08, 2010 6:50 pm
by Jonah Bron
That code is not very easy to understand quickly. I suggest you use one of the solutions I gave, probably the second one.
Re: diplaying date
Posted: Mon Nov 08, 2010 10:50 pm
by s.dot
You could use date_format() or the DateTime class
Code: Select all
$date = new DateTime('20101105');
echo $date->format('Y-m-d'); //2010-11-05
Re: diplaying date
Posted: Tue Nov 09, 2010 7:56 am
by Nomistake
Jonah Bron wrote:That code is not very easy to understand quickly. I suggest you use one of the solutions I gave, probably the second one.
yes

i will use your second one, also because maybe my solution demands more recources. Certainly because i use it in a while loop to generate a table...
thanks!