I want to retrieve date and time from server and according to it do some thing. For this I used following code:
$info = getdate(); $date = $info['mday']; $month = $info['mon']; $year = $info['year']; $hour = $info['hours']; $min = $info['minutes']; $sec = $info['seconds']; $current_date = "$date/$month/$year == $hour:$min:$sec";
This code returns proper date but problem is that what I see in my cpanel(server time) is diff. than what I get from code. In cpanel time is CDT while from code it is showing UTC which I reconfirm using following code
<?php echo date("r == e"); ?>
Why this is happening and what changes I have to do in my code so that I can get proper server time.
To display current date and time under Linux operating system using command prompt use the date command or timedatectl command. These commands can also display the current time / date in the given FORMAT. We can set the system date and time as root user too.
Usage Options. SQL Server provides several different functions that return the current date time including: GETDATE(), SYSDATETIME(), and CURRENT_TIMESTAMP. The GETDATE() and CURRENT_TIMESTAMP functions are interchangeable and return a datetime data type. The SYSDATETIME() function returns a datetime2 data type.
Answer: Use the PHP date() Function You can simply use the PHP date() function to get the current data and time in various format, for example, date('d-m-y h:i:s') , date('d/m/y H:i:s') , and so on.
You should set the timezone to the one of the timezones you want.
// set default timezone date_default_timezone_set('America/Chicago'); // CDT $info = getdate(); $date = $info['mday']; $month = $info['mon']; $year = $info['year']; $hour = $info['hours']; $min = $info['minutes']; $sec = $info['seconds']; $current_date = "$date/$month/$year == $hour:$min:$sec";
Or a much shorter version:
// set default timezone date_default_timezone_set('America/Chicago'); // CDT $current_date = date('d/m/Y == H:i:s');
Try this -
<?php date_default_timezone_set('Asia/Kolkata'); $timestamp = time(); $date_time = date("d-m-Y (D) H:i:s", $timestamp); echo "Current date and local time on this server is $date_time"; ?>
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