Jump to content

Adding Dates and Insert it..


fry2010

Recommended Posts

Hi, can someone show me how to add two dates together then insert into mysql database please?

I have read so many articles on it but they do not work for what I need.

Basically when I register a new user I need to find todays date and add 2 weeks to it, then insert into the mysql database.

I have tried to use the mysql function date_add() etc but it doesnt except it.

I am using it with PDO. So when I put it in my sql query i use this:

 

:expire

 

(:expire, '$expire');

 

$expire = date_add('DATETIME: AUTO CURDATE()', INTEVAL 7 DAYS);

 

this is what i am using, I no that it is my own error but I cant work it out so come here for help.

All the articles I have read just talk about SELECT statement NONE of them go into inserting an added date into mysql.

 

Why is it so dam hard to find information about doing stuff with dates? I have two books and neither of them talk much about using mysql dates. They aviod it. does anyone have a clue how to use dates with this? thanks.

Link to comment
https://forums.phpfreaks.com/topic/146746-adding-dates-and-insert-it/
Share on other sites

Here is what im using at the moment, I did have the CURTIME() thing before:

 


    $joined = date("Y-m-d");
    $add_expire = mktime(0,0,0,date("m"),date("d")+7,date("Y"));
    $expire = date("Y/m/d", $add_expire);

      $conn = db_connect();
      $conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);

      $sql = 'INSERT INTO useraccount VALUES
             (:user, :passwd, :salt, :email, :firstname, :surname, :expire, :joined)';

      $stmt = $conn->prepare($sql);
      $stmt->bindParam(':user', $user);
      $stmt->bindParam(':passwd', $passwd);
      $stmt->bindParam(':salt', $salt);
      $stmt->bindParam(':email', $email);
      $stmt->bindParam(':firstname', $firstname);
      $stmt->bindParam(':surname', $surname);
      $stmt->bindParam(':expire', $expire);
      $stmt->bindParam(':joined', $joined);
      $stmt->execute();

Also I do have the mysql table set up as:

 

create table useraccount (
username varchar(20) not null,
password char(50) not null,
salt char(30) not null,
email varchar(100) not null,
first_name varchar(50) not null,
surname varchar(50) not null,
expire date not null,
joined date not null,
PRIMARY KEY (username)
);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.