-1

How can I get the time without defining object from the below code?

<?php
    $localtime = new DateTime("now", new DateTimeZone('Asia/Dhaka'));
    echo $localtime->format('H:i:s');
?>
5
  • date('Y-m-d H:i:s') but using the DateTime class is considered the best practice. Commented Feb 1, 2015 at 5:34
  • Why don't you want to define the object? Beyond another object (or 2) in memory, I don't see the harm in it. Commented Feb 1, 2015 at 5:37
  • possible duplicate of Automatically detect user's current local time with JavaScript or PHP Commented Feb 1, 2015 at 5:44
  • Code may be duplicate but different way output has been wanted . And how can I get it ? Commented Feb 1, 2015 at 5:57
  • @Be0wulf The date/time classes can be very useful but they are not necessarily better in any way. Commented Feb 1, 2015 at 6:14

3 Answers 3

1

try this method

Here are various method choose your method and try

<?php
// Assuming today is March 10th, 2001, 5:16:18 pm, and that we are in the
// Mountain Standard Time (MST) Time Zone

$today = date("F j, Y, g:i a");                 // March 10, 2001, 5:16 pm
$today = date("m.d.y");                         // 03.10.01
$today = date("j, n, Y");                       // 10, 3, 2001
$today = date("Ymd");                           // 20010310
$today = date('h-i-s, j-m-y, it is w Day');     // 05-16-18, 10-03-01, 1631 1618 6 Satpm01
$today = date('\i\t \i\s \t\h\e jS \d\a\y.');   // it is the 10th day.
$today = date("D M j G:i:s T Y");               // Sat Mar 10 17:16:18 MST 2001
$today = date('H:m:s \m \i\s\ \m\o\n\t\h');     // 17:03:18 m is month
$today = date("H:i:s");                         // 17:16:18
$today = date("Y-m-d H:i:s");                   // 2001-03-10 17:16:18 (the MySQL DATETIME format)
?>
Sign up to request clarification or add additional context in comments.

1 Comment

Good idea to credit the source when you paste in examples from other sites (in this case, the PHP manual).
1

Like this:

date_default_timezone_set('Asia/Dhaka');
echo date(' H:i:s');

If you have access to the php.ini file then you should set the timezone there. Remember to restart the server.

date.timezone = "Asia/Dhaka"

There is nothing wrong with using the DateTime class, though.

Comments

0

Try this:

<?php
 $localtime = localtime();
 $localtime_assoc = localtime(time(), true);
 print_r($localtime);
 print_r($localtime_assoc);
?>

Outputs:

Array
(
    [0] => 24
    [1] => 3
    [2] => 19
    [3] => 3
    [4] => 3
    [5] => 105
    [6] => 0
    [7] => 92
    [8] => 1
)

Array
(
    [tm_sec] => 24
    [tm_min] => 3
    [tm_hour] => 19
    [tm_mday] => 3
    [tm_mon] => 3
    [tm_year] => 105
    [tm_wday] => 0
    [tm_yday] => 92
    [tm_isdst] => 1
)

The names of the different keys of the associative array are as follows:

[tm_sec] - seconds
[tm_min] - minutes
[tm_hour] - hour
[tm_mday] - day of the month
[tm_mon] - month of the year (January=0)
[tm_year] - Years since 1900
[tm_wday] - Day of the week (Sunday=0)
[tm_yday] - Day of the year
[tm_isdst] - Is daylight savings time in effect

See, if that helps.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.