Is there a PHP function that returns the date and time in the same format as the MySQL function NOW()
?
I know how to do it using date()
, but I am asking if there is a function only for this.
For example, to return:
2009-12-01 00:00:00
Is there a PHP function that returns the date and time in the same format as the MySQL function NOW()
?
I know how to do it using date()
, but I am asking if there is a function only for this.
For example, to return:
2009-12-01 00:00:00
Ok, after fiddling with this for some time, I withdraw the solution with date('U')
and suggest to use this one instead:
function isValidTimeStamp($timestamp)
{
return ((string) (int) $timestamp === $timestamp)
&& ($timestamp <= PHP_INT_MAX)
&& ($timestamp >= ~PHP_INT_MAX);
}
This check will only return true if the given $timestamp
is a string and consists solely of digits and an optional minus character. The number also has to be within the bit range of an integer (EDIT: actually unneeded as shown here).
var_dump( isValidTimeStamp(1) ); // false
var_dump( isValidTimeStamp('1') ); // TRUE
var_dump( isValidTimeStamp('1.0') ); // false
var_dump( isValidTimeStamp('1.1') ); // false
var_dump( isValidTimeStamp('0xFF') ); // false
var_dump( isValidTimeStamp('0123') ); // false
var_dump( isValidTimeStamp('01090') ); // false
var_dump( isValidTimeStamp('-1000000') ); // TRUE
var_dump( isValidTimeStamp('+1000000') ); // false
var_dump( isValidTimeStamp('2147483648') ); // false
var_dump( isValidTimeStamp('-2147483649') ); // false
The check for PHP_INT_MAX is to ensure that your string can be used correctly by date
and the likes, e.g. it ensures this doesn't happen*:
echo date('Y-m-d', '2147483648'); // 1901-12-13
echo date('Y-m-d', '-2147483649'); // 2038-01-19
On 64bit systems the integer is of course larger than that and the function will no longer return false for "2147483648" and "-2147483649" but for the corresponding larger numbers.
(*) Note: I'm not 100% sure, the bit range corresponds with what date can use though
The workaround is to use the hours
value as 24 hour clock. To represent '3:45:56 PM
', insert to the database column a string value '15:45:56'
.
Otherwise, you can use the STR_TO_DATE
function to convert the string into a valid TIME
value.
INSERT INTO mytable (mycol) VALUES (STR_TO_DATE('3:45:56 PM','%h:%i:%s %p'))
To retrieve a TIME
value from a database column, formatted as 12 hour clock with AM/PM.
SELECT DATE_FORMAT(mytimecol,'%h:%i:%s %p') AS mytimestr FROM ...
References:
STR_TO_DATE
https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_str-to-date
DATE_FORMAT
https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
I would setup the IPN listener to mark the item's status as sold in it's database table (you are generating that form based on a database?) when the IPN comes back "VERFIFIED" and payment_status = 'Complete'. Then, only generate that form when the item is not sold.
If you don't know how to implement an IPN listener, here is a tutorial: PayPal IPN with PHP
It's not a parameter of the query, in that you don't have to supply a value to MySQL.
$insert = $mysqli->prepare("INSERT INTO posts (post_name, publish_date) VALUES (?, NOW())");
Not besides the date function: