简体   繁体   中英

Php Mysql Date and Time from Database - just getting 0 or 1970

I'm really struggling and hope someone can please offer some advice.

I'm writing a simple web based system comprising of PHP and a Mysql database. I've tried so many ways to achieve what I want without success. All I want to do is simply store a date time in the database alongside each record, and be able to calculate when the record is 30 days old.

I don't know if the problem is due to the parameterized mysql binding or something else I'm doing wrong.

Problem 1:

Stored the record date in mysql as DATETIME field. But PHP's strToTime() function cant seem to ever understand the stored datetime - it always either returns 0 or 1970! All I'm storing in the database is from Php's Time() function. If I display the value directly on the page it is indeed a date - so I can't understand why strToTime() fails.

Problem 2: After spending hours on the above, I tried changing the mysql db field to an Integer type and storing the Unix Timestamp directly. But then for some reason when retrieving a stored value it will never cast properly as an int in order to perform calculations on it. I tried (int) cast and intval and both can't cast the value back to an int.. So hence Date() functions will not run on the returned value.

Problem 3: I tried using mysql's own function UNIX_TIMESTAMP() both on DATETIME field types and Int field Types, but even that comes back as a string that doesn't want to cast as an int in order to do calculations on.

It makes no sense at all. Clearly I'm doing something wrong but I can't figure out what.

//code im using to store the datetime:

 $mysqltime = time();
    if ($insert_stmt =  $mysqli->prepare("INSERT INTO quotes (quoteID, userID, creation_date) VALUES (?, ?, ?)")) {
        $insert_stmt->bind_param( 'sss', $quoteID, $_SESSION['user_id'], $mysqltime);

//code to demonstrate the problem I'm having retrieving the datetime stored:

if ($stmt = $mysqli->prepare("SELECT UID, title, cust, jtitle,     UNIX_TIMESTAMP(creation_date) FROM quotes
   WHERE userID = ? && status = ? 
    ")) {
    $stmt->bind_param('ss', $_SESSION['user_id'], $status );  // Bind to parameters.
    $stmt->execute();    // Execute the prepared query.
    $stmt->store_result();

    // get variables from result.
    $stmt->bind_result( $quoteID, $cust_title, $cust_surname, $job_title, $creation_date);
  //  $stmt->fetch();
 $results =  gmdate("d-m-Y", (int)$creation_date);

I personally prefer to use mysql over php for date and time management

Use an insert statement like this instead:

"INSERT INTO quotes (quoteID, userID, creation_date) VALUES (?, ?, now())"

and a select statement like:

"SELECT UID, title, cust, jtitle, DATE_FORMAT(creation_date,'%d %M %Y')as creation_date".
" FROM quotes WHERE userID = ? && status = ?"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM