diplaying 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
Nomistake
Forum Newbie
Posts: 15
Joined: Sat Oct 02, 2010 11:16 am

diplaying date

Post by Nomistake »

Hi,
A small question

How can i display

Code: Select all

$day = "20101105";
as a date format. (yyyy/mm/dd)

i tried something like this:

Code: Select all

echo date ("Y/m/d",$day);
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! :D
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: diplaying date

Post by Jonah Bron »

What you did is correct. The timestamp is apparently off. I tested that number, and I got the same results.
s992
Forum Contributor
Posts: 124
Joined: Wed Oct 27, 2010 3:06 pm

Re: diplaying date

Post 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

?>
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: diplaying date

Post by Jonah Bron »

Ooooohhhhh... it's not a timestamp. :roll:

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));
jojovem
Forum Newbie
Posts: 3
Joined: Sun Nov 07, 2010 11:56 am

Re: diplaying date

Post by jojovem »

mktime() wont help you?
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: diplaying date

Post 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.
Nomistake
Forum Newbie
Posts: 15
Joined: Sat Oct 02, 2010 11:16 am

Re: diplaying date

Post 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...

:)
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: diplaying date

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: diplaying date

Post 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
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Nomistake
Forum Newbie
Posts: 15
Joined: Sat Oct 02, 2010 11:16 am

Re: diplaying date

Post 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!
Post Reply