1

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.

3
  • What is not working? NOW() seems ok... Commented Dec 11, 2013 at 8:07
  • in colm2 2013-12-11 is inserted , but i like to insert Dec-11-2013 Commented Dec 11, 2013 at 8:09
  • 2
    You can take a look at the date() function. There are the possibilities listed. But why do you want to save it in this format? You can format it when you get the value from the database, but for storing it's the best method to save a date in sql. Commented Dec 11, 2013 at 8:09

7 Answers 7

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

Because it works with timezones. -- other PHP functions can also handle timezones just fine. +1 anyway, because this is the correct approach.
2

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');

Comments

1

Use DATE_FORMAT(NOW(),'%b %d %Y') .
For more info Date_Format

3 Comments

This is not a real answer. It may not contains only a link to documentation but also examples and explanations.
@OlivierH And why it is not a real answer? what is wrong in using Date_format. Please put some light
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.
1

I can see in your question that your db timezone format is YYYY-MM-DD

$date = date("Y-m-d", strtotime($yourDate)); 
$sql="insert into tbl_name (colm1,colm2) values (1,{$date})";

EDIT:

'Dec-10-2013' is known format for php, so you can do this $yourDate = 'Dec-10-2013';

Comments

1

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')";

1 Comment

You want M-d-Y instead. The date() documentation lists all the available format characters. ;)
1

you can try this

$date = date("M-d-Y");

$sql="insert into tbl_name (colm1,colm2) values (1,$date)";

Comments

1

first create the timestamp of today date

$date= strtotime("now");
insert into tablename value({$date});

To print the date again from database use this code

echo date("Y-m-d",$date);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.