Inserting date

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Darragh3277
Forum Newbie
Posts: 12
Joined: Tue Jun 24, 2008 9:19 am

Inserting date

Post by Darragh3277 »

Hi all, I'm pretty sure this is a simple problem but i'm pretty new and my head is nothing but addled so any help at all is greatly appreciated.

I have a database with a column date. I want to manually insert dates into it using a form and then be able to SORT BY date DESC.

In my form I just have a text box where I enter dd/mm/yyyy. Will sort by work using this or do I need to put it yyyy/mm/dd. Or am I just completly missing the mark?
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: Inserting date

Post by Zoxive »

Take a look at date();, strtotime();

Then in your database use the timestamp field type.

Code: Select all

 
$Timestamp = strtotime($Data);
 
$Time = date( 'Y-m-d H:i:s', $Timestamp);
 
// Now you can put $Time in your database because it is a formatted timestamp that MySql accepts.
 
Darragh3277
Forum Newbie
Posts: 12
Joined: Tue Jun 24, 2008 9:19 am

Re: Inserting date

Post by Darragh3277 »

Thanks for the reply. I am however after changing it slighty. Now instead of a plain text box for date I have 3 boxes. A drop down menu for day & month, and also a plain text field for year (YYYY). I was wondering if it were possible to do something like this:

Code: Select all

$Timestamp = strtotime($_POST[day], $_POST[month], $_POST[year]);
$Time = date( 'Y-m-d H:i:s', $Timestamp);
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: Inserting date

Post by Zoxive »

Darragh3277 wrote:Thanks for the reply. I am however after changing it slighty. Now instead of a plain text box for date I have 3 boxes. A drop down menu for day & month, and also a plain text field for year (YYYY). I was wondering if it were possible to do something like this:

Code: Select all

$Timestamp = strtotime($_POST[day], $_POST[month], $_POST[year]);
$Time = date( 'Y-m-d H:i:s', $Timestamp);

Code: Select all

 
$Merged = "{$_POST['day']}-{$_POST['month']}-{$_POST['year']}"; // One Format (of many) strtotime accepts
$Timestamp = strtotime($Merged);
$Time = date( 'Y-m-d H:i:s', $Timestamp);
 
// OR
 
$Timestamp = mktime(0,0,0,$_POST['month'],$_POST['day'],$_POST['year']);
$Time = date( 'Y-m-d H:i:s', $Timestamp);
 
 
Darragh3277
Forum Newbie
Posts: 12
Joined: Tue Jun 24, 2008 9:19 am

Re: Inserting date

Post by Darragh3277 »

Ah you legend!! Thanks for the help! Exactly what I was looking for! Cheers!
Post Reply