I need to set a variable to the current date in this format: eg: 2012-05-12 , I get that's YYYY-MM-DD ?
I've tried:
$date = date("yyyy-mm-dd", strtotime(now));
but this is not saving the date to mysql so it's coming out as 0000-00-00.
The field is set as type: date in the mysql db.
What am I doing wrong here?
$date = date("yyyy-mm-dd", strtotime(now));
Answer: Use the strtotime() Function You can first use the PHP strtotime() function to convert any textual datetime into Unix timestamp, then simply use the PHP date() function to convert this timestamp into desired date format. The following example will convert a date from yyyy-mm-dd format to dd-mm-yyyy.
The date_format() function returns a date formatted according to the specified format.
Easy:
$date = date('Y-m-d');
see http://php.net/manual/en/function.date.php
Always check the documentation, and you’ll find the format you’ll need is:
<?php date('Y-m-d'); ?>
EDIT: Also, you don’t need to specify the time as the second parameter, as date()
will use now by default. It should be noted that your syntax there is wrong too though, as it would be strtotime('now')
, as “now“ is a string it needs to be quoted.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With