How to insert today date in mysql using php.date format is like Dec-10-2013.i tried
$sql="insert into tbl_name (colm1,colm2) values (1,now())";
Here value inserted in colm2 is 2013-12-11.
How to insert today date in mysql using php.date format is like Dec-10-2013.i tried
$sql="insert into tbl_name (colm1,colm2) values (1,now())";
Here value inserted in colm2 is 2013-12-11.
What you want to do is save your dates as TIMESTAMP or something similar. When you retrieve it, you can format it. You can insert your dates with 'NOW()' or a given format if they are different.
If you want to display the time, you can do: new DateTime($row['my_date']);
For inserting you can use the same method: (new DateTime($date))->getTimestamp()
Why DateTime?
- Because it works with timezones.
Why not store it as Dec-10-2013?
- Because you cannot do anything with a varchar pretending to be a date. You have a TIMESTAMP field type for that
Because it works with timezones. -- other PHP functions can also handle timezones just fine. +1 anyway, because this is the correct approach.You can useDATE_FORMAT to format the date on the fly:
Change your query to:
INSERT INTO tbl_name (colm1,colm2)
VALUES (1, DATE_FORMAT(NOW(),'%b-%d-%Y'))
Example:
mysql> select DATE_FORMAT(NOW(),'%b-%d-%Y');
+-------------------------------+
| DATE_FORMAT(NOW(),'%b-%d-%Y') |
+-------------------------------+
| Dec-11-2013 |
+-------------------------------+
1 row in set (0.00 sec)
See the MySQL documentation for a list of available formatting options.
If you want to do it PHP-side, you can use date_create():
$date = date_create()->format('M-d-Y');
DATE_FORMAT is OK, no problem with that. This is a more the content of your answer which is problematic, even if you edited it. Have a look at this page, you will learn some interesting things. For examples, Links to external resources are encouraged, but please add context around the link so your fellow users will have some idea what it is and why it’s there. Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline.This is as close as I can get to your desired output right now, but I'll keep trying. Maybe if you replace the date input with a different letter, just try a bunch.
$CurDate = date("r");
$sql="INSERT INTO tbl_name (colm1,colm2)
VALUES (1,'$CurDate')";