close ad
Databridge V2 with MySQLi support IS Now Available!
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

rating

Date always 01-01-1970

Thread began 2/07/2012 11:51 am by gary.brett434358 | Last modified 2/14/2012 5:55 am by gary.brett434358 | 12293 views | 35 replies |

gary.brett434358

Date always 01-01-1970

Hi, this is most probably not an issue with with DA but has anyone come across this before, I usually work with SQL & asp so not sure?

I have fields which users can use a datepicker to enter a date. At top of each php page I have the following to allow uk format input but insert to db in its recognised values:

<?php 
if(isset($_POST['ContactDOB']) && $_POST['ContactDOB'] != "") {
$_POST['ContactDOB'] = date("Y-m-d", strtotime($_POST['ContactDOB']));
}
?>
<?php
if(isset($_POST['StatusAppDate']) && $_POST['StatusAppDate'] != "") {
$_POST['StatusAppDate'] = date("Y-m-d", strtotime($_POST['StatusAppDate']));
}
?>
<?php
if(isset($_POST['StatusActiveDate']) && $_POST['StatusActiveDate'] != "") {
$_POST['StatusActiveDate'] = date("Y-m-d", strtotime($_POST['StatusActiveDate']));
}
?>
<?php
if(isset($_POST['ContactCompDate']) && $_POST['ContactCompDate'] != "") {
$_POST['ContactCompDate'] = date("Y-m-d", strtotime($_POST['ContactCompDate']));
}
?>
<?php
if(isset($_POST['StatusNPWDate']) && $_POST['StatusNPWDate'] != "") {
$_POST['StatusNPWDate'] = date("Y-m-d", strtotime($_POST['StatusNPWDate']));
}
?>



However all fields dont have date selected write 01-01-1970 into table, is this normal? I have checked table and its set as 'Date', 'NULL' & allow NULL is checked.

Thanks

Sign in to reply to this post

Ian S

Dave has written a nice little function to create dates from a form POST.

showthread.php?t=17864

Cheers
Ian

Sign in to reply to this post

gary.brett434358

Thanks for your post Ian,
I removed my original code in the head section as posted above and inserted Dave's code. Then on the 3 date fields in my form I appended the code as such:

<?php echo ((isset($_POST["StatusActiveDate"]))?i8_formatUKDateMySQL($_POST["StatusActiveDate"]):""); ?>
<?php echo ((isset($_POST["StatusCompDate"]))?i8_formatUKDateMySQL($_POST["StatusCompDate"]):""); ?>
<?php echo ((isset($_POST["StatusNPWDate"]))?i8_formatUKDateMySQL($_POST["StatusNPWDate"]):""); ?>



When I now open the form and insert date or if the fields are left blank it inserts '0000-00-00' into each field in the database, in the details page however it just returns 01-01-1970!!

Any ideas where I am going wrong, I really need the fields to be blank if nothing is entered, yet accept uk value if datepicker is used?

Insert page attached if it helps anyone.

Thanks again

Attached Files
tblcases_Insert_Direct.zip
Sign in to reply to this post

Ian S

They way that I handle dates, using a transform function like Daves is to put them into Session Values and then insert the session values into the database. There might be a neater way than this though....

Here is a quick example:

<?php
if (isset($_POST["BT_Add"])) { // Trigger
if ($_POST['dateup'] != '') {
$_SESSION['var_update'] = formatUKDateMySQL($_POST['update']);
} else {
$_SESSION['var_update'] = Null;
}
if ($_POST['datedown'] != '') {
$_SESSION['var_downdate'] = formatUKDateMySQL($_POST['downdate']);
} else {
$_SESSION['var_downdate'] = Null;
}
}
?>

Basically the code above checks to see if the date field is populated. If it is it transforms the date to UK and if not then it passes a null value. That way I get either a UK date or blank field.

Hope this helps?

Cheers
Ian

Sign in to reply to this post

gary.brett434358

Hi Ian, thanks again for the help.
Do you use your code on an insert/update page? Do I simply need to add your code to top of the page and nowhere else?

Using Dave's code I had to add code to values as previous post?

Thanks

Sign in to reply to this post

gary.brett434358

Hi again Ian, just tried putting your code at top of page and removed the i8 code from insert behaviour values. Do I need to reference the fieldnames anywhere?

This still inserts 0000-00-00 into MySQL and displays it as 01-01-1970 on the details page? As you can probably tell I know nothing of php/mysql, normally work in SQL & asp which seems straightforward right now!

insert.php

<?php
if (isset($_POST["BT_Add"])) { // Trigger
if ($_POST['dateup'] != '') {
$_SESSION['var_update'] = formatUKDateMySQL($_POST['update']);
} else {
$_SESSION['var_update'] = Null;
}
if ($_POST['datedown'] != '') {
$_SESSION['var_downdate'] = formatUKDateMySQL($_POST['downdate']);
} else {
$_SESSION['var_downdate'] = Null;
}
}
?>



Example date field

<input type="text" name="StatusActiveDate" id="StatusActiveDate" value="Set Live Date" size="32" class="datepicker" /



details.php

[code]
<tr> <td><h6>Case Active:</h6></td>
<td><?php echo(date("d-m-Y", strtotime($row_WADAtblcases['StatusActiveDate']))); ?></td>
</tr>
<tr> <td><h6>Case Completed:</h6></td>
<td><?php echo(date("d-m-Y", strtotime($row_WADAtblcases['StatusCompDate']))); ?></td>
</tr>
<tr> <td><h6>Case NPW:</h6></td>
<td><?php echo(date("d-m-Y", strtotime($row_WADAtblcases['StatusNPWDate']))); ?></td>
</tr>
[code]

Sign in to reply to this post

Ian S

You will still need Daves code (or equivalent).

If you are using Daves code you need to put my code between your insert / update behaviour and Daves code.

So with Daves code you would use

<?php
if (isset($_POST["BT_Add"])) { // Trigger assuming that your submit button is called BT_Add
if ($_POST['StatusActiveDate'] != '') {
$_SESSION['StatusActiveDate'] = i8_formatUKDateMySQL($_POST['StatusActiveDate']);
} else {
$_SESSION['StatusActiveDate'] = Null;
}
}
?>

And then the values that you would pass to the database are stored in the Session variables.

Cheers
Ian

Sign in to reply to this post

gary.brett434358

Thanks again Ian, its becoming a long day! My form button is set via a js file so I don't have a submit button name as such?

On my insert page I have now added Daves function above your code, 1 each for the date fields on my page. This still inserts 0000-00-00 into database rather than null and still displays 01-01-1970 on results page.

Is this because I have no submit button to reference do you think?

<?php
function i8_formatUKDateMySQL($date) {
$d = str_replace(" ","",$date);
$d = explode('/', $date);
$d = $d[2].'-'.$d[1].'-'.$d[0];
return $d;
}
?>
<?php
if (isset($_POST["doc_upload"])) { // Trigger assuming that your submit button is called BT_Add
if ($_POST['StatusActiveDate'] != '') {
$_SESSION['StatusActiveDate'] = i8_formatUKDateMySQL($_POST['StatusActiveDate']);
} else {
$_SESSION['StatusActiveDate'] = Null;
}
}
?>
<?php
if (isset($_POST["doc_upload"])) { // Trigger assuming that your submit button is called BT_Add
if ($_POST['StatusCompDate'] != '') {
$_SESSION['StatusCompDate'] = i8_formatUKDateMySQL($_POST['StatusCompDate']);
} else {
$_SESSION['StatusCompDate'] = Null;
}
}
?>



Thanks again..

Sign in to reply to this post

Ian S

I think it could well be because the PHP code is set to run if the form is submitted via a post.

Can you try it with a standard form POST and see if that works?

Sign in to reply to this post

gary.brett434358

Originally Said By: Ian S
  I think it could well be because the PHP code is set to run if the form is submitted via a post.

Can you try it with a standard form POST and see if that works?  



Hi Ian, the form was already POST, what I have done as a test in use a submit button called insert. All form data is submitted as usual but the date field I added a value to & the date fields left blank were written as 0000-00-00 again.

On the update.php page if I select a date on 1 field it does write it into the db & I can clear the 2 other date fields by delete 01-01-1970, this works fine.

Just to clarify the code on the update.php page that inserts the correct date is

<?php 
if(isset($_POST['ContactDOB']) && $_POST['ContactDOB'] != "") {
$_POST['ContactDOB'] = date("Y-m-d", strtotime($_POST['ContactDOB']));
}
?>



This is so confusing!

Sign in to reply to this post
loading

Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

Build websites from already-built web applications

These out-of-the-box solutions provide you proven, tested applications that can be up and running now.  Build a store, a gallery, or a web-based email solution.

Want your website pre-built and hosted?

Close Windowclose

Rate your experience or provide feedback on this page

Account or customer service questions?
Please user our contact form.

Need technical support?
Please visit support to ask a question

Content

rating

Layout

rating

Ease of use

rating

security code refresh image

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.

Close Windowclose

We were unable to retrieve the attached file

Close Windowclose

Attach and remove files

add attachmentAdd attachment
Close Windowclose

Enter the URL you would like to link to in your post

Close Windowclose

This is how you use right click RTF editing

Enable right click RTF editing option allows you to add html markup into your tutorial such as images, bulleted lists, files and more...

-- click to close --

Uploading file...