Convert time from unix to regular format. What every programmer should know about time. Converting seconds to days, hours and minutes

Because There are no built-in tools in diesel fuel, we create the following script: #!/bin/sh truss date 2>&1 |grep ^time |awk "(print $3;)" exit $? or nawk "BEGIN(print srand())" or in Perl: perl -e "print time, "\n";" find out the file modification date: truss -v lstat -t lstat ls -l file.txt 2>&1 1>/dev/null | grep "mt\ =\ " | awk "(print $9;)"

How to get Unix time in...

Perl time
PHP time()
Ruby Time.now (or Time.new). To output: Time.now.to_i
Python import time first, then time.time()
Java long epoch = System.currentTimeMillis()/1000;
Microsoft .NET C# epoch = (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
VBScript/ASP DateDiff("s", "01/01/1970 00:00:00", Now())
Erlang calendar:datetime_to_gregorian_seconds(calendar:now_to_universal_time(now()))-719528*24*3600.
MySQL SELECT unix_timestamp(now())
PostgreSQL SELECT extract(epoch FROM now());
SQL Server SELECT DATEDIFF(s, "1970-01-01 00:00:00", GETUTCDATE())
JavaScript Math.round(new Date().getTime()/1000.0) getTime() returns the time in milliseconds.
Unix/Linux date +%s
Other OS Command line: perl -e "print time" (If Perl is installed on your system)

Converting date to Unix time in...
PHP mktime( watch, minutes, seconds, month, day, year)
Ruby Time.local( year, month, day, watch, minutes, seconds, usec) (or Time.gm for GMT/UTC output). To output add.to_i
Python import time first, then int(time.mktime(time.strptime("2000-01-01 12:34:00", "%Y-%m-%d %H:%M:%S")))
Java long epoch = new java.text.SimpleDateFormat("dd/MM/yyyy HH:mm:ss").parse("01/01/1970 01:00:00");
VBScript/ASP DateDiff("s", "01/01/1970 00:00:00", date field)
MySQL SELECT unix_timestamp( time) Time format: YYYY-MM-DD HH:MM:SS or YYMMDD or YYYYMMDD
PostgreSQL SELECT extract(epoch FROM date("2000-01-01 12:34"));
With timestamp: SELECT EXTRACT(EPOCH FROM TIMESTAMP WITH TIME ZONE "2001-02-16 20:38:40-08"); With interval: SELECT EXTRACT(EPOCH FROM INTERVAL "5 days 3 hours");
SQL Server SELECT DATEDIFF(s, "1970-01-01 00:00:00", date field)
Unix/Linux date +%s -d"Jan 1, 1980 00:00:01"

Converting Unix times to human readable dates...
PHP date( Format, unix time);
Ruby Time.at( unix time)
Python import time first, then time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.localtime( unix time)) Replace time.localtime with time.gmtime for GMT date.
Java String date = new java.text.SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(new java.util.Date ( unix time*1000));
VBScript/ASP DateAdd("s", unix time, "01/01/1970 00:00:00")
PostgreSQL SELECT TIMESTAMP WITH TIME ZONE "epoch" + unix time* INTERVAL "1 second";
MySQL from_unixtime( unix time, optional, output format) Standard output format YYY-MM-DD HH:MM:SS
SQL Server DATEADD(s, unix time, "1970-01-01 00:00:00")
Microsoft Excel =(A1 / 86400) + 25569 The result will be in the GMT time zone. For other time zones: =((A1 +/- time difference for the zone) / 86400) + 25569.
Linux date -d @1190000000
Other OS Command line: perl -e "print scalar(localtime( unix time))" (If Perl is installed) Replace "localtime" with "gmtime" for the GMT/UTC time zone.

What is Unix time or Unix epoch (Unix epoch or Unix time or POSIX time or Unix timestamp)?

UNIX time or POSIX time is a time encoding method adopted in UNIX and other POSIX-compatible operating systems.
The starting point is considered to be midnight (UTC) from December 31, 1969 to January 1, 1970, the time from this moment is called the “UNIX era” (English Unix Epoch).
UNIX time is consistent with UTC, in particular, when UTC leap seconds are declared, the corresponding second numbers are repeated.
The method of storing time in the form of a number of seconds is very convenient to use when comparing dates (accurate to the second), as well as for storing dates: if necessary, they can be converted to any human-readable format. Date and time in this format also takes up very little space (4 or 8 bytes, depending on the size of the machine word), so it is reasonable to use it for storing large amounts of dates. Performance disadvantages can occur when date elements, such as month numbers, etc., are accessed very frequently. But in most cases, it is more efficient to store time as a single value rather than a collection of fields.

Converting Unix epoch to human readable date


Unix start and end date of year, month or day


Converting seconds to days, hours and minutes


How to get Unix time in...

Perltime
PHPtime()
RubyTime.now (or Time.new). To output: Time.now.to_i
Pythonimport time first, then time.time()
Javalong epoch = System.currentTimeMillis()/1000;
Microsoft .NET C#epoch = (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
VBScript/ASPDateDiff("s", "01/01/1970 00:00:00", Now())
Erlangcalendar:datetime_to_gregorian_seconds(calendar:now_to_universal_time(now()))-719528*24*3600.
MySQLSELECT unix_timestamp(now())
PostgreSQLSELECT extract(epoch FROM now());
SQL ServerSELECT DATEDIFF(s, "1970-01-01 00:00:00", GETUTCDATE())
JavaScriptMath.round(new Date().getTime()/1000.0) getTime() returns the time in milliseconds.
Unix/Linuxdate +%s
Other OSCommand line: perl -e "print time" (If Perl is installed on your system)

Converting date to Unix time in...

PHPmktime( watch, minutes, seconds, month, day, year)
RubyTime.local( year, month, day, watch, minutes, seconds, usec) (or Time.gm for GMT/UTC output). To output add.to_i
Pythonimport time first, then int(time.mktime(time.strptime("2000-01-01 12:34:00", "%Y-%m-%d %H:%M:%S")))
Javalong epoch = new java.text.SimpleDateFormat("dd/MM/yyyy HH:mm:ss").parse("01/01/1970 01:00:00");
VBScript/ASPDateDiff("s", "01/01/1970 00:00:00", date field)
MySQLSELECT unix_timestamp( time) Time format: YYYY-MM-DD HH:MM:SS or YYMMDD or YYYYMMDD
PostgreSQLSELECT extract(epoch FROM date("2000-01-01 12:34"));
With timestamp: SELECT EXTRACT(EPOCH FROM TIMESTAMP WITH TIME ZONE "2001-02-16 20:38:40-08"); With interval: SELECT EXTRACT(EPOCH FROM INTERVAL "5 days 3 hours");
SQL ServerSELECT DATEDIFF(s, "1970-01-01 00:00:00", date field)
Unix/Linuxdate +%s -d"Jan 1, 1980 00:00:01"

Converting Unix times to human readable dates...

PHPdate( Format, unix time);
RubyTime.at( unix time)
Pythonimport time first, then time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.localtime( unix time)) Replace time.localtime with time.gmtime for GMT date.
JavaString date = new java.text.SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(new java.util.Date ( unix time*1000));
VBScript/ASPDateAdd("s", unix time, "01/01/1970 00:00:00")
PostgreSQLSELECT TIMESTAMP WITH TIME ZONE "epoch" + unix time* INTERVAL "1 second";
MySQLfrom_unixtime( unix time, optional, output format) Standard output format YYY-MM-DD HH:MM:SS
SQL ServerDATEADD(s, unix time, "1970-01-01 00:00:00")
Microsoft Excel=(A1 / 86400) + 25569 The result will be in the GMT time zone. For other time zones: =((A1 +/- time difference for the zone) / 86400) + 25569.
Linuxdate -d @1190000000
Other OSCommand line: perl -e "print scalar(localtime( unix time))" (If Perl is installed) Replace "localtime" with "gmtime" for the GMT/UTC time zone.

What is the "Unixtime Converter" tool needed for?

This tool will, first of all, be useful to webmasters who constantly deal with large volumes of dates or often refer to their elements in their work. Using the "Unixtime Converter" tool, you can easily convert Unix time into a user-friendly date (and vice versa), find out the current Unix epoch time, and also get Unix time in various programming languages, DBMSs and operating systems.

What is Unix time?

The Unix era (Unix epoch) began on the night of December 31, 1969 to January 1, 1970. It was this date that was taken as the starting point of “computer” time, which is calculated in seconds and takes up very little disk space - only 4 or 8 bytes. With this encoding method, programmers can "hide" any date into a single number, and easily convert it back into a format that users can understand.

Unix time (also called Unix time or POSIX time) is convenient to use in various operating systems and programming languages, since it is displayed as a single value rather than a certain number of fields taking up space. In addition, UNIX time fully complies with the UTC standard (including in leap years) - in this case, the corresponding second values ​​are simply repeated.

Unix Terminology

A few words about terms.

So, Unix time(or POSIX time) is the number of seconds that have passed from midnight on January 1, 1970 to the present.

Unix Timestamp(time stamp) is a “fixed” time, in other words, a specific date imprinted in a number.

UTC(Universal Coordinated Time) is the Coordinated Universal Time, which is “fixed” at the prime meridian, and from which geographic time zones are counted.

How “durable” is this system?

In just a couple of decades, namely on January 19, 2038 at 03:14:08 UTC, Unix time will reach the value 2147483648, and computer systems can interpret this number as negative. The key to solving this problem is to use a 64-bit (instead of 32-bit) variable to store the time. In this case, the supply of numerical values ​​of Unix time will last humanity for another 292 billion years. Not bad, right?

Unix time is the same for everyone

If you live in London or San Francisco, and your friends live in Moscow, then you can “check your watches” using Unix time: this system is currently the same for the whole world. Naturally, if the time on the servers is set correctly. And with the help of a tool "Unixtime converter" This conversion will take you a fraction of a second.

Only for Lifeexample readers it is possible to open an online store on Moguta.CMS with a 15% discount

Unix time and Unix Timestamp (MySQL, PHP, JavaScript)

Hello, dear blog readers, in this article, I want to tell you about what it is Unix time And Unix Timestamp. Programmers often combine these concepts into one, but this is not entirely true. In addition, the article contains many useful notes on working with Unix Timestamp in PHP, MySQL and JavaScript.

Why Unix time starts on January 1, 1970

The thing is that Unix time begins counting the Unix era with the release of the first UNIX system. The first system of this kind was created in 1969, so the developers took the date January 1, 1970 at midnight UTC ( UTC).

Let's understand what Unix Time and Unix Timestamp are for and give them clear concepts.

Unix Timestampis a timestamp that is a sequence of characters representing the number of seconds that have passed since January 1, 1970.

I’ll try to give an example to clarify the difference between these two concepts.

At the time of writing this post, Unix time was equal 1346765877 .

While you are reading this information, a record of time ( 1346765877 ) is already a label - Unix Timestamp! Converting this timestamp into human-readable form, we get the date 04-09-2012 and time 17:37:57.

Frankly speaking, in my opinion, there is no particular point in separating the two concepts, but it is still useful to have an idea of ​​​​what it is Unix Time, and it is also useful to understand that the number of maximum possible seconds that have passed since 1970 has a limit!

The end of the Unix era will come in 2038

Fact: the maximum binary number in 32-bit systems is the number 01111111 11111111 11111111 11111111 , converting it to the decimal system, we get the number 2147483647.

January 19, 2038 at 03:14:08 there will come a moment when the number of seconds that have passed since the beginning of the Unix era exceeds the maximum available in a 32-bit system, number = 2147483647. If the bit overflows, the date will be reset.

It’s very easy to test this theory with a clear example:

  • Open a standard Windows calculator, press ALT+3, this will convert it to engineering view;
  • Set 4 byte mode, and decimal input type;
  • Write the number 2147483647;

  • Pay attention to the representation of the number in binary;
  • Add one to the number;

  • The result of addition will be a negative number!

If we continue adding one, we get a cyclic closure.

It is this date ringing that will occur from January 19, 2038 on all systems using 32-bit architecture.

In fact, there is no need to be sad, because computer system developers are increasingly introducing 64-bit architectures into widespread use. Let's believe that they will make it by 2038.

Now let's talk about using unix timestamp in php, mysql and even in javascript.

Working with unix timestamp

A very important point when working with unix timestamp in php or mysql is the need to clearly understand the pros and cons of this date format.

For example, TIMESTAMP cannot be used to specify historical events or events of the distant future. The entire set of dates is limited to the period from 1970 to early 2038. If you set a date beyond 2038, it will not be correctly interpreted by the 32-bit system.

Realizing this limitation, a logical question arises: " Why bother representing dates in seconds?"

When to Use Unix Timestamp

To represent time in our usual system of measuring it, 8 bytes are required, and for a unix timestamp it is half as much - 4 bytes.

Saving data volume, in my opinion, is the main and undeniable advantage of using Unix Time.

In addition, there are a number of useful nuances available when working with UNIX timestamp in mysql. And since all information must be stored on the database server, and it, in turn, has a number of advantages when working with Unix timestamps, the choice towards unix timestamp can be correctly justified by the following provisions.

MySQL provides a corresponding Timestamp data type for working with the Unix time format, by installing which we immediately get a useful advantage over standard formats DATE And DATETIME. The advantage is that when you add a new record to a table, the column with that data type is automatically populated. This means that we can save not only on the amount of data, but also on the CPU time of the web server.

To back up the word with action, we will set the following task: when registering a new user in the system, you need to enter the date of its addition to the database.

If the type of the field storing the date in the table is DATETIME, then the request from the PHP script will look something like this:

The benefits are obvious!

There is also a minus: if there are several fields of the TIMESTAMP type, only the first one is automatically updated.

Does it make sense to use INT instead of timestamp

Many programmers, when working with unix timestamp, use the integer format int(11) . This is a completely irrational approach to the issue, since MySQL provides many useful functions for the timestamp type that affect the speed of working with it. Therefore, by storing the timestamp in INT, we will deprive ourselves of all server support for this format. This is approximately the same as storing id in the varchar(11) type.

However, there is one justification for keeping unix timestamp to INT. When transferring a database between different DBMSs, a type conflict may arise, i.e. For one of the DBMSs, the timestamp type may be unfamiliar. In this case, using int will have an advantage, since this format is available in all DBMSs.

Brief description of MySQL calendar types

TIMESTAMP- data type for storing date and time. The data is stored as the number of seconds that have passed since the start of the “Unix era”. Value range: 1970-01-01 00:00:00 - 2038-12-31 00:00:00. Occupies 4 bytes.

DATE- data type for storing dates. Value range: 1000-01-01 - 9999-12-31. Occupies 3 bytes.

DATETIME- data type for storing date and time. Value range: 1000-01-01 00:00:00 - 9999-12-31 00:00:00. It takes 8 bytes and is stored as a number YYYYMMDDHHMMSS./p>

YEAR- data type for storing the year. Value range: 1901 - 2155. Occupies 1 byte.

TIME- data type for storing time. Value range: −828:59:59 - 828:59:59. Occupies 3 bytes.

Date conversion in unix

It's time to post some useful functions for converting dates to unix timestamp and back from unix-time on a readable date.

How to get the current UNIX time

  • PHP:

    time();

  • JavaScript:

    Math.round(new Date().getTime()/1000.0);

  • MySQL:

    SELECT unix_timestamp(now()) ;

This tool is needed to convert a date from the Unix TimeStamp format to a human-readable date and vice versa.

What is Unix time and what is it used for? To understand why this is used, I’ll start with the general concept of what Unix time is.

Unix time (or TimeStamp, which translated into Russian means “time stamp” and has the same meaning) is the number of seconds that have passed since January 1, 1970. That is, the Unix TimeStamp at the time of 01/01/1970 00:00:00 was equal to 0. After 2 minutes (120 seconds), the Unix time was already 120. For example, a day later (01/02/1970 00:00:00) the Unix time was is already equal to 86400, since 60*60*24=86400 seconds have passed. Now the Unix Time Stamp is already 1566148027 and the number is constantly growing, since the seconds are constantly ticking.

But why use it? The thing is that Unix TimeStamp is convenient to use for storing and manipulating dates when programming. I won’t go into details, but in short, a number is much more convenient to count and compare than a string with “left” characters. This is why most developers use Unix TimeStamp to work with dates in their projects, and in the database we often see one very large number in the `date` field, which does not look like a date at all.

This is where this tool comes in handy. With it, you can easily translate that “big number from the database” into a human-readable date. Plus, you can even do the opposite and turn any date into a Unix TimeStamp. These are the capabilities this converter is endowed with.

Problem of 2038

As I already said, the number Unix TimeStamp with every second it increases by 1. Sooner or later the limit of this number must come and it will be in 2038. The thing is that the maximum number in the 32-bit operating systems common at the beginning of the 21st century is 2 31. This is the number that Unix TimeStamp will reach in 2038.

→ A solution to this problem has already been found. To ensure that sites do not stop tracking time correctly in 2038, it is enough to use a 64-bit operating system on the hosting/VDS/dedicated server, and not a 32-bit one. With the rapidly growing power of computers and the decrease in their cost, everything is heading towards the fact that by 2038 the vast majority of services in the field of providing space for a website will be provided on the basis of 64-bit OS. By the way, in a 64-bit system, such a problem will not affect us for at least 292 billion years, which is quite enough to consider problem of 2038 solved.