Welcome Guest, Not a member yet? Register   Sign In
strtotime problem!
#1

[eluser]newbie boy[/eluser]
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.

here's my code for the controller:

Code:
$data['day']    = $this->input->post('day');
$data['month']    = $this->input->post('month');
$data['year']    = $this->input->post('year');

here's my code for the model:

Code:
$calendar  = $data['day']."/".$data['month']."/".$data['year'];
$date      = explode("/",$calendar);
        
$this->GeneralInfo->birthday    = strtotime($date[0] .$date[1] . $date[2]);
#2

[eluser]fesweb[/eluser]
Easiest thing is to check the submitted date, using php checkdate():
Code:
if( checkdate($data['month'], $data['day'], $data['year'] )
{
$this->GeneralInfo->birthday = strtotime($date[0] .$date[1] . $date[2]);
}
else
{
$this->GeneralInfo->birthday = ''; // or NULL -- whatever solves your problem.
}
Something like that anyway...
#3

[eluser]newbie boy[/eluser]
sir gradstudent, i tried your solution before but it never worked out.
coz' this gives somewhat a validation to the field that must have a complete input.
i do appreciate your help.

but . . .

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...
#4

[eluser]fesweb[/eluser]
Oh - in that case, you don't want to store actual dates at all!!

Just use 3 fields in the database: birth_day, birth_month, birth_year. Each has 0 as a default.

No need to use strtotime() at all in this situation. Just insert what the user gives you, and output it by only showing the fields which aren't 0's.

If you want to display actual month names, just create an array of those and access it:
Code:
$months = array(
  1 => 'January',
  2 => 'February',
  3 => 'March',
  // etc...
);

if($row->birth_month > 0)
{
echo $months[$row->birth_month];
}
// etc...
#5

[eluser]umefarooq[/eluser]
hi take full date from user and ask him date format while saving it to database how he want to view his DOB, than you can use this format to display the date. just check mysql date formats its really easy to dispaly.
#6

[eluser]Sarfaraz Momin[/eluser]
Well for a birthday maybe the year is not required but the day and month is still required. What I would do is format the date using something more complex but I am sure it would gimme good results.

Code:
strtotime(date('Y-m-d',mktime(0,0,0,04,06,1978)))

Have a good day !!!




Theme © iAndrew 2016 - Forum software by © MyBB