Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get current datetime using PHP

Tags:

php

datetime

I want to add content to a MySQL table with current date and time.
When I insert the content to database it shows the correct date but the wrong time.

$date1 = date("Y-m-d H:i:s"); 
like image 534
Naveen K Avatar asked Jul 29 '26 20:07

Naveen K


2 Answers

date_default_timezone_set('Asia/Kolkata'); 

$date1 = date("Y-m-d H:i:s"); 

Asia/calcutta has changed to Asia/Kolkata

like image 131
Devnegikec Avatar answered Aug 01 '26 10:08

Devnegikec


[a] strftime(): Format a local time/date according to locale settings

[b] date : Format a local time/date

Examples

Consider following simple example:

<?php
print strftime('%c');
?>

Output:

Mon Apr 23 01:22:58 2007

You need to pass format such as %c to strftime() to print date and time representation for the current system. You can use following format characters:

%m - month as a decimal number (range 01 to 12) %d - day of the month as a decimal number (range 01 to 31) %Y - year as a decimal number including the century You can see the complete format conversion specifiers online here You can also use date() as follows:

<?php
 print date('r');
 print "\\n";
 print date('D, d M Y H:i:s T');
 print "\\n";
?>

Output:

Mon, 23 Apr 2007 01:29:56 +0530
Mon, 23 Apr 2007 01:35:14 IST
like image 29
Nirav Ranpara Avatar answered Aug 01 '26 11:08

Nirav Ranpara



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!