Welcome! Log In Create A New Profile

Advanced

Leading zeros and the dob field

Posted by PeterJ 
Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
avatar Leading zeros and the dob field
October 10, 2011 08:54PM
Im a bit worried about leading zeros and the dob field
Say for instance you use a loop to generate your days of the month drop down
the numbers 1 -9 will be single digit ,when concatenated with the month and year , this will make the dob variable less than 8 characters
Ie 1st of june 2009 will be 1062009 or even 162009

now with my student_edit page, to display the correct dob ,the value needs to be 8 characters from the dob field ie 01062009 otherwise it goes all out of wack
So what i want to know Mac, if you have preregistered students in your database, do their dobs have leading zeros in front of single digit numbers making all dob values 8 characters?
hope this makes sense
avatar Re: Leading zeros and the dob field
October 11, 2011 07:20AM
Hi PeterJ,

The column dob is a varchar so leading zero's wont be removed.

Try this:

Language: PHP
$days = range(1, 31); $months = range(1, 12);   echo ';<b>Days :</b><br />';; foreach($days as $day){ if(strlen($day) == 1){ echo ';0';.$day.';<br />';; } else { echo $day.';<br />';; } }   echo ';<b>Months :</b><br />';; foreach($months as $month){ if(strlen($month) == 1){ echo ';0';.$month.';<br />';; } else { echo $month.';<br />';; } }

Hope this helps smiling smiley

student no: 77315138
Re: Leading zeros and the dob field
October 11, 2011 12:29PM
Hey,

My date is saved as ddmmyyyy (11102011).

Is that the correct format or is it yyyymmdd?

I'm using a function that depends on this format.

72565330
avatar Re: Leading zeros and the dob field
October 11, 2011 02:09PM
Hi Chantal,

It doesn't really matter in which format you save it, as long as you use it correctly smiling smiley I have mine saved as ddmmyyy also thumbs up

student no: 77315138
Re: Leading zeros and the dob field
October 11, 2011 02:49PM
I am using the strtotime function.
For this function my date needs to be in the yyyymmdd format.

strtotime

Quote
To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat() when possible.
avatar Re: Leading zeros and the dob field
October 11, 2011 03:09PM
This is how I do it :

Language: PHP
//JOIN Day-Month-Year $dob = "12051987";   echo "Day: ".substr($dob, 0, 2)."<br />"; echo "Month: ".substr($dob, 2, 2)."<br />"; echo "Year: ".substr($dob, 4, 4);

student no: 77315138
Re: Leading zeros and the dob field
October 11, 2011 03:15PM
I would still like to know which format is the right one to use?

Quote
It doesn't really matter in which format you save it, as long as you use it correctly I have mine saved as ddmmyyy also

Your code will also give an error if Mac's dob is saved as yyyymmdd.
Re: Leading zeros and the dob field
October 11, 2011 03:17PM
I'm obviously referring to the possible preregistered students , PeterJ was talking about.

72565330
Re: Leading zeros and the dob field
October 11, 2011 03:20PM
We really need clarification on this, mine is in the format yy-mm-dd. The field on the table on the dbase is `dob` varchar(8) NOT NULL default '', that should translate to 8 character...???? Is that not right????
avatar Re: Leading zeros and the dob field
October 11, 2011 07:06PM
Ahhh Riaz!!
Ok firstly that loop is awesome, so neat
I did this
Language: PHP
if ($day_of_birth < 10){ $day_of_birth = "0". $day_of_birth ; }

secondly I am pretty happy u did the substr thing, mine is like this

Language: PHP
$day_of_birth = substr("$dob",0 , -6); $month_of_birth = substr("$dob", 2, -4); $year_of_birth = substr("$dob", -4);

Riaz perhaps you can see the problem of there being preregistered students with a date format that will not be substringed as we have thought...
assuming preregistered students exist
avatar Re: Leading zeros and the dob field
October 12, 2011 07:27AM
Hey PeterJ,

I don't think this is going to work:

Language: PHP
if ($day_of_birth < 10){ $day_of_birth = "0". $day_of_birth ; }

You need to get length of $day_of_birth :

Language: PHP
if (strlen($day_of_birth) < 10){ $day_of_birth = "0". $day_of_birth ; }

But that still isn't going to work, take for example:

Language: PHP
$day_of_birth = "162009";   if ($day_of_birth < 10){ $day_of_birth = "0". $day_of_birth ; }   echo $day_of_birth //outputs: 0162009

The output is adding a zero to the front(day) and not the month. Unless you already have a zero added to the month then your solution should work fine smiling smiley

student no: 77315138
avatar Re: Leading zeros and the dob field
October 12, 2011 07:29AM
@peterj

The substr solution I don't think we should have a problem, mac will probably clear his students table (if we lucky) then run our portfolios smiling smiley

student no: 77315138
avatar
Mac
Re: Leading zeros and the dob field
October 12, 2011 08:15AM
The format of the date does not matter in this case given that it is varchar. You use whatever format you want. Glad to see the issue being discussed here.... this is logic being tested smiling smiley There are so many options available here... was the mysql field a date field, then it would be different.
avatar Re: Leading zeros and the dob field
October 12, 2011 06:01PM
Riaz my day_of_birth is a variable generated from my loop that generates days 1-31
Im adding the leading 0 before i concatenate the day month and year
so if the day is a single digital, a 0 is added with my conditional, then the values are concatenated and inserted
Sorry, only registered users may post in this forum.

Click here to login