Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change Date Format

Status
Not open for further replies.

georgeocrawford

Technical User
Aug 12, 2002
111
GB
Hi,

I am using a php script to write from a form to my MySql database.

Just a few things not quite perfect:

1.
The form has three seperate select fields for date input: day (1-31), month (Jan-Dec) and year (2002-2005).

I am using the following line to create a variable 'sqldate' which is posted to the database in the form YYYY-MM-DD:

$sqldate = "$year-$month-$day";

...but I need to change the month values so they are in the format '01-12' rather than 'Jan-Dec'. How can I do this?

2.
A similar problem.

My 'address' field is generated using the following line, and is a combination of 4 fields in the form separated by a comma:

$address = "$address1, $address2, $address3, $postcode";

This works perfectly when all 4 fields are filled-in, but when the user omits 'address2' and 'address3', for example, I get an entry like this:

"1 The Street, , , The Postcode"

How can I omit the space and the comma from '$address' when the field is left blank?

Thanks for your help!
 
1) Change your form to use values between 1 and 12 instead of Jan-Dec
2) This should work:
Code:
$addressarr = array($address1, $address2, $address3, $postcode);
$address = implode(", ", $addressarr);
//Daniel
 
Thanks again for your help Daniel.

The second solution sounds perfect.

However, I would rather avoid changing the month values to 1-12, as it doesn't look that good on the form.

Is there any way of mapping Jan to 01, Feb to 02, Mar to 03, etc?

George
 
You could do this:
Code:
$months = array('Jan' => 1, 'Feb' => 2, 'Mar' => 3, 'Apr' => 4, 'May' => 5, 'Jun' => 6, 'Jul' => 7, 'Aug' => 8, 'Sep' => 9, 'Oct' => 10, 'Nov' => 11, 'Dec' => 12);
$sqldate = "$year-" . $months[$month] . "-$day";
//Daniel
 
Make your select box like this:

<select name=&quot;s_month&quot;>
<option value=&quot;01&quot;>Jan</option>
<option value=&quot;02&quot;>Feb</option>
<option value=&quot;03&quot;>Mar</option>
<option value=&quot;04&quot;>Apr</option>
<option value=&quot;05&quot;>May</option>
<option value=&quot;06&quot;>Jun</option>
<option value=&quot;07&quot;>Jul</option>
<option value=&quot;08&quot;>Aug</option>
<option value=&quot;09&quot;>Sep</option>
<option value=&quot;10&quot;>Oct</option>
<option value=&quot;11&quot;>Nov</option>
<option value=&quot;12&quot;>Dec</option>
</select>

This way the user sees months, but numeric values are passed as the value. ______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Thanks guys.

The date thing is sorted now.

However, the implode function doesn't seem to be working.

I am still getting results like &quot;George, , , London&quot;, without any errors coming up.

I copied your code exactly, Daniel.

Any suggestions?
 
Actually, I forgot the most important part of the snippet.
Code:
$addressarr = array($address1, $address2, $address3, $postcode);
$address = implode(&quot;, &quot;, $addressarr);
should be
[coe]$addressarr = array($address1, $address2, $address3, $postcode);
foreach ($addressarr as $key => $val)
{
if (empty($val))
unset($addressarr[$key]);
}
$address = implode(&quot;, &quot;, $addressarr);[/code] //Daniel
 
Hi,

Could you possibly check this code once again - I'm getting:

Code:
parse error, unexpected T_STRING

when I try and run it.

Here is the code I'm using:

Code:
$addressarr = array($address1, $address2, $address3, $postcode);
foreach ($addressarr as $key => $val)
{
    if (empty($val))
        unset($addressarr[$key]);
}
$address = implode(&quot;, &quot;, $addressarr);

The error is on the
Code:
unset
line.

Thanks
 
This works fine on my system with Apache 2.0.40 and PHP 4.2.2. What version of PHP are you using? //Daniel
 
I'm using 4.2.2 too.

I'm getting more and more convinced it's a typo of some sort, but I don't have a good enough understanding of php to find it. Could you possibly paste the code again, and check it for errors?

Sorry to be a pain!

George
 
I used the exact code you had in your post.
This is the script I used to test it:
Code:
<?php
$address1 = &quot;Test&quot;;
$address2 = &quot;&quot;;
$address3 = &quot;Test&quot;;
$postcode = &quot;test&quot;;
$addressarr = array($address1, $address2, $address3, $postcode);
foreach ($addressarr as $key => $val)
{
    if (empty($val))
        unset($addressarr[$key]);
}
$address = implode(&quot;, &quot;, $addressarr);
echo $address;
?>
This outputs &quot;Test, Test, test&quot; on my system. //Daniel
 
I'm sorry Daniel, but I've been trying to crack this for hours!

Here is a link to my phpinfo page:


Could you take a look and see if there is an obvious explanation why this simple script doesn't work?!!!

Failing that, mabye you could suggest another way round the problem which avoids unset?

Thanks again,

George
 
Here is a quick'n'dirty workdaround:
Code:
<?php
$address = &quot;&quot;;
if (!empty($address1))
    $address .= $address1 . &quot;, &quot;;
if (!emtpy($address2))
    $address .= $address2 . &quot;, &quot;;
if (!empty($address3))
    $address .= $address3 . &quot;, &quot;;
if (!empty($postcode))
    $address .= $postcode;
$address = substr($address, 0, -1);
?>
//Daniel
 
Hi Daniel,

Thought you might like an update on my errors with using this simple little script!

I used this script, copied directly from your post:

Code:
$addressarr = array($address1, $address2, $address3, $postcode);
foreach ($addressarr as $key => $val)
{
    if (empty($val))
        unset($addressarr[$key]);
}
$address = implode(&quot;, &quot;, $addressarr);

HOWEVER, what I didn't realise was that somewhere along the line, the spaces (or tabs) you has inserted on the
Code:
if
and the
Code:
unset
lines had been turned into other, invisible characters, which did not show up in Dreamweaver, yet caused the errors in the tested php script!

So
Code:
        unset($addressarr[$key]);

became
Code:
________unset($addressarr[$key]);

Just thought u might like to know in case anyone else has problems in the future!

George
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top