Page 1 of 1

Date Insertion

Posted: Sat Aug 09, 2008 7:34 pm
by CoolAsCarlito
Is there a way when you have a php script that when you fill out a little web form to create a member and have it sent to a database it'll also take the date of when the user was created and store that date in the database as well?

Re: Date Insertion

Posted: Sat Aug 09, 2008 7:49 pm
by EverLearning
If you're using DATETIME field in the database you can use MySql function NOW() directly in your query or PHP date() function to format the date appropriately. if you save time in DB as a timestamp, you can use php time(). Just make sure your PHP timezone and MySql timezone settings are in sync.

Re: Date Insertion

Posted: Sat Aug 09, 2008 10:23 pm
by CoolAsCarlito
Okay I'm lost. I tried to follow but just to make sure you understand what I'm wanting. I'm sure you know what I'm going to say but I'm going to describe it again. What I mean is when the new record is created it also posts the date it was created and inserted into the row called "date created".

Re: Date Insertion

Posted: Sat Aug 09, 2008 10:31 pm
by EverLearning
Like I said if your date_created field is, for example, DATETIME type you can use

Code: Select all

$sql = "INSERT INTO products 
        (name, description, date_created)
    VALUES
        ('{$name}', '{$description}', NOW())";
// or
$date_created = date('Y-m-d H:i:s');
$sql = "INSERT INTO products 
        (name, description, date_created)
    VALUES
        ('{$name}', '{$description}', '{$date_created}')";
 

Re: Date Insertion

Posted: Sat Aug 09, 2008 10:50 pm
by CoolAsCarlito
Ahh thank you

Re: Date Insertion

Posted: Wed Aug 13, 2008 11:47 am
by CoolAsCarlito
Okay so I'm trying to put the date as (month day, year)

And I put this as the syntax:
$query = "SELECT * FROM titlehistory FORMAT datecreated ('m-d-Y')";


Could not retrieve the data because You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'datecreated (m-d-Y)' at line 1. The query was $query.

Can you tell me why it's bringing that error?

Re: Date Insertion

Posted: Wed Aug 13, 2008 12:10 pm
by EverLearning
Take a look at the MySql DATE_FORMAT() function.

Re: Date Insertion

Posted: Wed Aug 13, 2008 12:23 pm
by CoolAsCarlito
//Define the query
$query = "SELECT * FROM titlehistory FORMAT datecreated (%m %e,%Y)";

Still gives me the same result.

Re: Date Insertion

Posted: Wed Aug 13, 2008 12:32 pm
by EverLearning
When i said "Take a look", I meant READ. The link I gave you has several examples of DATE_FORMAT() function use.