Php cli get time

How do I get the local system time in PHP?

I’m writing a PHP system and I need to get the system time. Not the GMT time or the time specific to a timezone, but the same system time that is used by the CRON system. I have a CRON job that runs every day at midnight and I want to show on a webpage how long will it take before it runs again. For example: Right now it is 6pm on my system clock. I run the code:

$timeLeftUntilMidnight = date("H:i", strtotime("tomorrow") - strtotime("now")); 

Let me try to clarify: if I run date(«H:i», time()), it will show me a time that is 3 hours from now. Because of that, strtotime(«tomorrow») — strtotime(«now») is giving me 3 hours less than correct. For example, now it is 6pm, and the cronjob will run at 12am, but strtotime(«tomorrow») — strtotime(«now») is returning 3 hours, because «tomorrow» is 00:00 but «now» is 9pm.

10 Answers 10

There are many answers, however there is not even one correct at the time of writing.

Читайте также:  Передача значений переменных

PHP time() function doesn’t return the system time, like most folks believe, but it return the PHP localtime, normally set with date.timezone in php.ini, or set with date_default_timezone_set() within a script.

For instance in one of my servers, PHP time was set to Europe/Rome and system time to UTC . I had a difference of one hour between system time and PHP time.

I’m going to give you a solution that works for Linux, I don’t know for Windows. In Linux the system timezone is set in /etc/timezone . Now, this is normally outside my allowed open_basedir setting, but you can add :/etc/timezone to your list to be able to read the file.

Then, on top of the scripts, that want to get the system time, you can call a library function that sets the script timezone to the system timezone. I suppose that this function is part of a class, so I use static:

static function setSystemTz() < $systemTz = trim(file_get_contents("/etc/timezone")); if ($systemTz == 'Etc/UTC') $systemTz = 'UTC'; date_default_timezone_set($systemTz); >

To make the matter worse in PHP 5.3.3 ‘Etc/UTC’ is not recognized, while ‘UTC’ is, so I had to add an if to fix that.

Now you can happily call time() and it will really give you the system time. I’ve tested it, because I needed it for myself, that’s why I found this question now.

Источник

PHP: How to get current time in hour:minute:second?

In order to get the date in the right format I want I used date(«d-m-Y») . Now I want to get the time in addition to the date in the following format H:M:S How can I procede ?

3 Answers 3

Anytime you have a question about a particular function in PHP, the easiest way to get quick answers is by visiting php.net, which has great documentation on all of the language’s capabilities.

Looking up a function is easy, just visit http://php.net/ and it will forward you to the appropriate place. For the date function, we’ll visit http://php.net/date.

We immediately learn a couple things about this function by examining its signature:

string date ( string $format [, int $timestamp = time() ] ) 

First, it returns a string. That’s what the first string in the above code means. Secondly, the first parameter is expected to be a string containing the format. There is an optional second parameter for passing in your own timestamp (to construct strings from some time other than now).

date("d-m-Y") // produces something like 03-12-2012 

In this code, d represents the day of the month (with a leading 0 is necessary). m represents the month, again with a leading zero if necessary. And Y represents the full 4-digit year. All of these are documented in the aforementioned link.

To satisfy your request of getting the hours, minutes, and seconds, we need to give a quick look at the documentation to see which characters represents those particular units of time. When we do that, we find the following:

h 12-hour format of an hour with leading zeros 01 through 12 i Minutes with leading zeros 00 to 59 s Seconds, with leading zeros 00 through 59 

With this in mind, we can no create a new format string:

date("d-m-Y h:i:s"); // produces something like 03-12-2012 03:29:13 

Hope this is helpful, and I hope you find the documentation has benefiting to your development as I have to mine.

Источник

Get the Current Date and Time in PHP

Get the Current Date and Time in PHP

  1. Use date() and time() Function to Get the Current Date and Time in PHP
  2. Use DateTime Object to Get the Current Date and Time in PHP

In this article, we will introduce methods to get the current date and time in PHP.

Use date() and time() Function to Get the Current Date and Time in PHP

We could use built-in functions date() and time() to get the current date and time in PHP. These functions display the current date and time irrespective of the time zone. The correct syntax to use these two functions is as follows.

It returns the date that is set according to the specified format.

The time() function accepts no parameters. It returns the current PHP timestamp. The timestamp is the number of seconds calculated since the Unix Epoch.

The program below shows how to get the current date and time irrespective of the time zone.

php $DateAndTime = date('m-d-Y h:i:s a', time()); echo "The current date and time are $DateAndTime."; ?> 

The format «m-d-Y h:i:s a» specifies the returned date with month, day and 4-digit year value, and the time in hours, minutes, and seconds.

The current date and time are 05-20-2020 05:44:03 pm. 

One thing to note here is that the time is not set according to some time zone. It is the time displayed using Unix timestamp. If you want to check the current PHP time based on your time zone, you need to set the time zone manually.

For example, if you want to check the current time in Amsterdam, you have to set the time zone for Amsterdam manually. For this purpose, the PHP function date_default_timezone_set() is used. The correct syntax to use this function is as follows

date_default_timezone_set($timezone); 

It accepts only one parameter.

This function returns a bool value. If the time zone is set successfully, it returns true ; otherwise, it returns false .

The program below will display the time after setting the time zone.

php date_default_timezone_set('Europe/Amsterdam'); $DateAndTime = date('m-d-Y h:i:s a', time()); echo "The current date and time in Amsterdam are $DateAndTime.\n"; date_default_timezone_set('America/Toronto'); $DateAndTime2 = date('m-d-Y h:i:s a', time()); echo "The current date and time in Toronto are $DateAndTime2."; ?> 
The current date and time in Amsterdam are 05-20-2020 08:08:42 pm. The current date and time in Toronto are 05-20-2020 02:08:42 pm. 

Use DateTime Object to Get the Current Date and Time in PHP

In PHP, DateTime class is used to deal with the problems related to the date and time . The format() function of this class displays the date and time in a specified format.

We will first create a DateTime object and then call format() function to display the date and time.

$ObjectName = new DateTime(); $ObjectName->format($format); 

The example program that displays the current date and time is as follows.

php $Object = new DateTime(); $DateAndTime = $Object->format("d-m-Y h:i:s a"); echo "The current date and time are $DateAndTime."; ?> 

The output is the date and time with the specified format. The time displayed is the time calculated since the Unix Epoch.

The current date and time are 20-05-2020 06:21:06 pm. 

If you want to check the time according to a specific time zone, you will have to set the time zone manually. For this purpose, the PHP built-in function of DateTime class setTimezone() is used. The correct syntax to use this function is as follows.

$ObjectName = new DateTime(); $ObjectName->setTimezone(DateTimezone $timezone); 

The example program that displays the time according to a specific time zone is as follows.

php $Object = new DateTime(); $Object->setTimezone(new DateTimeZone('Europe/Amsterdam')); $DateAndTime = $Object->format("d-m-Y h:i:s a"); echo "The current date and time in Amsterdam are $DateAndTime.\n"; $Object->setTimezone(new DateTimeZone('America/Toronto')); $DateAndTime = $Object->format("d-m-Y h:i:s a"); echo "The current date and time in Toronto are $DateAndTime."; ?> 
The current date and time in Amsterdam are 20-05-2020 08:43:02 pm. The current date and time in Toronto are 20-05-2020 02:43:02 pm. 

Related Article — PHP Date

Related Article — PHP DateTime

Источник

How to Get the Current Date and Time in PHP

For getting the current Date and Time in PHP, it is recommended to use the date() function or the DateTime() class.

This short tutorial will assist you in getting the current date and time in PHP. Below you can find the two options we recommend to use.

Applying the date() Function

The PHP date() function is capable of converting the timestamp, located in the computer memory in a readable format.

Let’s, first, check out the syntax of the date() function:

 $timestamp = time(); // get the current timestamp $date = date('Y-m-d H:i:s', $timestamp); // format the timestamp as a string in the desired format echo $date; // output the formatted date and time ?>

Finally, the output you will get is as follows:

Applying the DateTime() Class

An alternative and efficient way of getting the date and time is using the DateTime() class. Also, it is possible to apply functions for formatting the date and time in various ways.

Here is how to get the date and time with DateTime class:

 $currentDate = new DateTime(); echo $currentDate->format('Y-m-d H:i:s'); ?>

The output is something as follows:

Describing Date and Time in PHP

The manipulation of date and time is an inevitable part of programming. To be able to work with data and time, one should be aware of the arsenal of the PHP tools.

Date and time functions allow getting the date and time from the server where PHP scripts run. These functions are applied for formatting date and time in various ways.

Источник

Оцените статью