Date Insertion

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
CoolAsCarlito
Forum Contributor
Posts: 192
Joined: Sat May 31, 2008 3:27 pm
Contact:

Date Insertion

Post 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?
User avatar
EverLearning
Forum Contributor
Posts: 282
Joined: Sat Feb 23, 2008 3:49 am
Location: Niš, Serbia

Re: Date Insertion

Post 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.
CoolAsCarlito
Forum Contributor
Posts: 192
Joined: Sat May 31, 2008 3:27 pm
Contact:

Re: Date Insertion

Post 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".
User avatar
EverLearning
Forum Contributor
Posts: 282
Joined: Sat Feb 23, 2008 3:49 am
Location: Niš, Serbia

Re: Date Insertion

Post 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}')";
 
CoolAsCarlito
Forum Contributor
Posts: 192
Joined: Sat May 31, 2008 3:27 pm
Contact:

Re: Date Insertion

Post by CoolAsCarlito »

Ahh thank you
CoolAsCarlito
Forum Contributor
Posts: 192
Joined: Sat May 31, 2008 3:27 pm
Contact:

Re: Date Insertion

Post 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?
User avatar
EverLearning
Forum Contributor
Posts: 282
Joined: Sat Feb 23, 2008 3:49 am
Location: Niš, Serbia

Re: Date Insertion

Post by EverLearning »

Take a look at the MySql DATE_FORMAT() function.
CoolAsCarlito
Forum Contributor
Posts: 192
Joined: Sat May 31, 2008 3:27 pm
Contact:

Re: Date Insertion

Post by CoolAsCarlito »

//Define the query
$query = "SELECT * FROM titlehistory FORMAT datecreated (%m %e,%Y)";

Still gives me the same result.
User avatar
EverLearning
Forum Contributor
Posts: 282
Joined: Sat Feb 23, 2008 3:49 am
Location: Niš, Serbia

Re: Date Insertion

Post by EverLearning »

When i said "Take a look", I meant READ. The link I gave you has several examples of DATE_FORMAT() function use.
Post Reply