strtotime problem!

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
melchor_06
Forum Newbie
Posts: 13
Joined: Wed Feb 11, 2009 3:58 am

strtotime problem!

Post by melchor_06 »

my strtotime works perfectly fine when a user inputs his birthday which has the day, the month and the year.
but when a user did not input his day, month and year of his birthday.
same if its incomplete or the user had only input just one data or two.
still in the database it will put a value of 0.
and when it's echoed, it will ouput 01 Jan 1970.

how can i solve this?
so that if a user did not input his birthday, the database will just be blank.
and also if the user only input the day, still it will it corretly ouput it.
same goes if only the month or the year nor if one of its data has no input,
still it would correctly ouput the two data correctly.

Code: Select all

 
$calendar = $data['day']."/".$data['month']."/".$data['year'];
$date      = explode("/",$calendar);
        
$birthday = strtotime($date[0] .$date[1] . $date[2]);
 
echo $birthday;
 
thanks guys...
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: strtotime problem!

Post by mattpointblank »

You need to either stop the data reaching the database when it's inserted, or filter it when it comes out. Something like:

Code: Select all

 
if($_POST['day'] == 0) {
 // either don't display a date, or set it to null in the insert query
}
 
I don't really understand how you can display a date without all the parts...?
melchor_06
Forum Newbie
Posts: 13
Joined: Wed Feb 11, 2009 3:58 am

Re: strtotime problem!

Post by melchor_06 »

sir mattpointblank, i tried your solution before but it never worked out.

some user just wants to give either the only the day, or the month or the year of their birthday
nor the combination of the said data mostly only the day and the month but not all...

but still i want to display it without making a function for a user to use if he wants to hide the day, the month, mostly the year
or either the combination of the two data.

so if he do not want to show any of the data of his birthday, he'll just not have to input any...
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Re: strtotime problem!

Post by Bill H »

You are npot going to be able to do that using a "date" field or a timestamp. Those database items are designed to hold an entire date. A timestamp holds the number of seconds elapsed since Jan 1, 1970. If you want to allow entry of partial dates you will need three fields, one for eact part of the date and will need to store them separately.
Post Reply