Forum

Thread tagged as: Question, Problem

Storing value of Date Field in DB

I'm trying to store the value of a 'Date' field in the Database.

I need to be able to test to see if the date stored is in the past or not.

So far, I've tried storing it in a column that's a DATE column, or a VARCHAR column - neither receive any data (presumably not formatted the way the columns are expecting).

Darren Cusdin

Darren Cusdin 0 points

  • 5 years ago

so this is how perch stores the date info in the database

          `dateCreated` datetime NOT NULL DEFAULT '2013-01-01 00:00:00',

you should store your dates as Y-m-d H:i:s as this would be consistent with how perch stores the same information.

Rachel Andrew

Rachel Andrew 394 points
Perch Support

If you use a date template tag we will store the value correctly for you. What are you trying to achieve outside of Perch functionality?

If you can explain what the task is you need to do and include code and your Diagnostics Report as requested on the thread creation page, we'll be in a better position to help.

Robert Ketter said: you should store your dates as Y-m-d H:i:s as this would be consistent with how perch stores the same information.

Hi Robert,

I'm using the Perch Form API class, which I think might be making this difficult.

Here is my code for the datepicker field:

echo $Form->date_field('dateDate', 'Date', isset($details['dateDate'])?$details['dateDate']:false);

The column 'dateDate' is datetime NOT NULL DEFAULT '2013-01-01.

At the moment, it's still not storing the value in the DB - any ideas?

Thanks!

Drew McLellan

Drew McLellan 2638 points
Perch Support

Are you using a template? If not and you're hardcoding your fields, you'll need to make sure you receive the date field:

$data = $Form->receive($postvars);
$data['dateDate'] = $Form->get_date('dateDate');