-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');
?>
Gaurav Dave
  • 6,838
  • 9
  • 25
  • 39
Neo
  • 31
  • 1
  • 4
  • `date('Y-m-d H:i:s')` but using the `DateTime` class is considered the best practice. – Be0wulf Feb 01 '15 at 05: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. – Turnerj Feb 01 '15 at 05:37
  • possible duplicate of [Automatically detect user's current local time with JavaScript or PHP](http://stackoverflow.com/questions/863474/automatically-detect-users-current-local-time-with-javascript-or-php) – sanoj lawrence Feb 01 '15 at 05:44
  • Code may be duplicate but different way output has been wanted . And how can I get it ? – Neo Feb 01 '15 at 05:57
  • @Be0wulf The `date/time` classes can be very *useful* but they are not necessarily *better* in any way. – Sverri M. Olsen Feb 01 '15 at 06:14

3 Answers3

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)
?>
sanoj lawrence
  • 951
  • 5
  • 29
  • 69
  • 1
    Good idea to [credit the source](http://php.net/manual/en/function.date.php#example-2405) when you paste in examples from other sites (in this case, the PHP manual). – Turnerj Feb 19 '15 at 03:41
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.

Sverri M. Olsen
  • 13,055
  • 3
  • 36
  • 52
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.

Gaurav Dave
  • 6,838
  • 9
  • 25
  • 39