Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Set Now to format YYYY-MM-DD

Tags:

php

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?

like image 569
Satch3000 Avatar asked Nov 13 '12 13:11

Satch3000


People also ask

How can I get current date in YYYY MM DD format in PHP?

$date = date("yyyy-mm-dd", strtotime(now));

How convert date from yyyy mm dd to dd-mm-yyyy format in PHP?

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.

Which PHP function do you use to format date information?

The date_format() function returns a date formatted according to the specified format.


2 Answers

Easy:

$date = date('Y-m-d');

see http://php.net/manual/en/function.date.php

like image 55
Grüse Avatar answered Sep 21 '22 20:09

Grüse


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.

like image 40
Martin Bean Avatar answered Sep 18 '22 20:09

Martin Bean