dimazaid
dimazaid

Reputation: 1671

TimeStamp in mysql and php

am trying create a database for a use with a subscription and expiration date,I added the subscription date in my database as a Timestamp field, and the expiration date should be after a month..am trying to add a month but the functions i found are only working with date format functions,i thought they should be the same(Timestamp and date() ) but its giving me an error!

$startDate = time();

$conv = date('Y-m-d H:i:s',$startDate);

echo $conv;

echo ('<br/>');

$dd=date('Y-m-d H:i:s', strtotime('+1 month', $startDate));

echo $dd;

this function works just fine but if I replace the start date with the value 'TIMESTAMP' i saved in the database it gives me this error" A non well formed numeric value encountered any suggestions?! Thanks in advance

Upvotes: 1

Views: 638

Answers (2)

Wh1T3h4Ck5
Wh1T3h4Ck5

Reputation: 8509

$startDate = strtotime(TIMESTAMP_FROM_MYSQL);

Upvotes: 2

Michael J.V.
Michael J.V.

Reputation: 5609

MySQL TIMESTAMP isn't displayed as integer like it is in PHP. If you check the documentation, you'll see that TIMESTAMP is displayed using 19 characters. If you want to store number of seconds in your database - use INT (unsigned) type rather than TIMESTAMP.

In other words, run strtotime($mysql_timestamp) (replace variable name with appropriate one) before you use your code so it gets converted to an integer from string.

However, you should really consider whether you made the right choices when it comes to date storage - there are some very interesting functions to work with dates built in both in PHP and MySQL. If you try to handle it all manually (via number of seconds from 1970) you'll run into many problems (timezone offsets, DST, not being able to store dates BEFORE 1970 and so on).

Upvotes: 2

Related Questions