php time difference between current time and database time [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have database. I save my value on 1480079589 time.
I want to measure time between current time and my database time which I saved.
I don't have any idea because of I dın't understand time logic. 1480079589 is what time ?

To calculate time difference
SELECT timestamp AS 'thisisit'
FROM table
WHERE TIMESTAMPDIFF(MINUTE, timestamp, NOW()) <= 15;
This is a timestamp.
Use PHP's date() function.
Example:
echo date('m/d/Y', 1480079589);
Simply put, the Unix timestamp is a way to track time as a running
total of seconds. This count starts at the Unix Epoch on January 1st,
1970 at UTC. Therefore, the Unix timestamp is merely the number of
seconds between a particular date and the Unix Epoch. It should also
be pointed out that this point in time technically does not change no
matter where you are located on the globe. This is very useful to
computer systems for tracking and sorting dated information in dynamic
and distributed applications both online and client side. The reason
why Unix timestamps are used by many webmasters is because they can
represent all time zones at once.
Please refer to this for more info:
http://php.net/manual/en/datetime.settimestamp.php
Refer to this question too:
What is a Unix timestamp and why use it?

1480079589 is timestamp, that is number of seconds from the time landmark (1970-01-01 00:00:00). That means 1480079589 seconds away from the landmark.
date('Y-m-d H:i:s', 1480079589);
that will print the date in a friendly way for you to see

That`s UNIX Time.
The unix time stamp is a way to track time as a running total of seconds.
This count starts at the Unix Epoch on January 1st, 1970 at UTC.
Therefore, the unix time stamp is merely the number of seconds between a particular date and the Unix Epoch.
It should also be pointed out (thanks to the comments from visitors to this site) that this point in time technically does not change no matter where you are located on the globe. This is very useful to computer systems for tracking and sorting dated information in dynamic and distributed applications both online and client side.
To get time difference try this:
<?php
$databaseTime = 1480079589; // Time from DataBase
print(time() - $databaseTime); // How much second

The time format is seconds since midnight 1/1/1970. Also known as Unix Time or timestamp.
You get this is php with the time() function. To measure time passed you could just compare two timestamps and get the seconds inbetween.
You convert a timestamp to a human readable format using
gmdate("M d Y H:i:s", $unixTimestamp);
date("M d Y H:i:s", $unixTimestamp);

I use
DATE_SUB(NOW(),INTERVAL 5 MINUTE)
in sql query. and my problem is solved.

Related

Convert date format 1542575966120 18/11/2018 [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 4 years ago.
I have a DB that shows when the user made the last login but then it shows 1542575966120. I wanted it to show so 18/11/2018 19:00
I tried using this in php
$intDate = "20". $ infologado ["lastlogin"];
$newDate = date ("d-m-Y", strtotime ($ intDate));
but I could not.
sorry for English
So as #Taha Paksu had mentioned, these numbers are a timestamp (seconds since 1 January 1970). Try this code:
$intDate = 1542575966120;
$newDate = date('d/m/Y H:i', $intDate/1000);
It is in miliseconds, date function accepts seconds, thus the division by 1000. Also no need to put it into strtotime, because this function is meant to convert string dates to... said numeric timestamps.
In your case, you can put $intDate = $infologado['lastlogin']; instead of first line to get the result dynamically from the database.
First of all, you need to learn what a timestamp is. The timestamp is a number which shows the seconds passed (or milliseconds, some include the milliseconds too) since epoch (01/01/1970). A general definition can be found here:
The Unix epoch (or Unix time or POSIX time or Unix timestamp) is the number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT), not counting leap seconds (in ISO 8601: 1970-01-01T00:00:00Z). Literally speaking the epoch is Unix time 0 (midnight 1/1/1970), but 'epoch' is often used as a synonym for 'Unix time'. Many Unix systems store epoch dates as a signed 32-bit integer, which might cause problems on January 19, 2038 (known as the Year 2038 problem or Y2038).
The converter on this page converts timestamps in seconds, milliseconds and microseconds to readable dates.
Taken from: https://www.epochconverter.com/ a tool which you can convert your dates to/from timestamps or vice versa.
Then to answer your question, the system saved the dates as a timestamp to the database to (probably) bypass the formatting errors on each different system that uses it.
Nevermind, TL;DR:
The number shows Sunday, 18 November 2018 21:19:26.120 when you give it to the timestamp converter I mentioned above. With PHP, you can use:
$unixTimestamp = 1542575966120;
$dt = DateTime::createFromFormat("U.u", $unixTimestamp / 1000);
var_dump($dt);
to convert to PHP DateTime class, then you can use it in your application.

Set GMT+8 PHP Time [duplicate]

This question already has answers here:
Dealing with timezones in PHP
(4 answers)
Closed 9 years ago.
I have some php scripts on a hosting, but hosting time is different from my local time (GMT+8)
How set the right time() script to be GMT+8 ?
When i use:
<?
echo time(); //it show me the hosting time;
?>
time() will always return the number of seconds since the epoch. The code below will print the same twice.
date_default_timezone_set('Europe/London');
echo time();
date_default_timezone_set('America/Cuiaba');
echo time();
The concept of Unix Timestamp does not carry time zone information by design. A given timestamp is always the same regardless of time zone. (The number of seconds since 1970-01-01 00:00:00 UTC) When you want to express a timestamp with time zone taken into account, you will adjust the resulting date with the current time zone's offset.
So when using the 'c' format option to PHP's date (which does reflect time zone information) you will see different representation of the same timestamp
date_default_timezone_set('Europe/London');
echo time();
echo date('c')
date_default_timezone_set('America/Cuiaba');
echo time();
echo date('c');
Will output:
1384259474
2013-11-12T12:31:14+00:00
1384259474
2013-11-12T09:31:14-03:00
Your assumption is not correct:
int time ( void )
Returns the current time measured in the number of seconds since the
Unix Epoch (January 1 1970 00:00:00 GMT).
The Unix Epoch is a fixed moment in time. If you really get an invalid timestamp, your hosting provider has not cared to set the server's clock.
If you want to do decent time zone aware date handling I suggest you learn about the DateTime class and friends and:
Use named time zones (Europe/Madrid) rather than UTC offsets (+01:00) since they take DST into account.
Set your app's time zone as default so you don't need to specify it every time:
date_default_timezone_set("Europe/Helsinki");
Never ever do date math yourself (e.g., don't add 86400 seconds manually to increase a day).
You must set the local time in php
function date_default_timezone_set
List of Supported Timezones
Example:
date_default_timezone_set("America/Fortaleza");
echo time(); // Local Time in America/Fortaleza
Add 28800 (8 hrs converted to seconds) to the output of time()
<?php echo (time()+28800); ?>
I am not sure but, time() returns timestamp which is equivalent to GMT thus, adding timestamp of 8 hours will give you GMT+8. Similarly, you can even subtract the time also
You can use date_default_timezone_set() function, For example
date_default_timezone_set('Asia/Dhaka');

Extracting server time with PHP and storing in MySQL - Unix time stamp OK?

All,
I'm trying to decide how to deal with time in a project which relies on (server) time intervals (in short, some content is available after user completed a specific action at least n hours before). Right now, it seems like the easiest option would be to extract the Unix time stamp with time() and store it as is in MySQL.
Any reason why this is not a good idea? Any gotcha I need to be aware of? Performance impact?
Timestamps are fine. Don't divide them, it's unneeded calculation. If you plan to query (per object) about a timeout more often than update it then you would be better off storing the expiration time instead of the current (so calculating delta only once). Beware about DATETIME columns: they don't regard timezone setting, while your PHP does... so if you happen to have different timezone settings on different requests, then you're out of luck. Timestamps are absolute, and they also account for manace like daylight-savings times, where 3:01 is 2 minutes after 1:59...
Seems fine to me. Though you should probably store it as a DATETIME and use DateTime objects, rather than UNIX timestamps and time().
$time = new DateTime;
echo $time->format("Y-m-d H:i:s"); //Outputs current time, example: 2012-10-13 22:58:34
Actually, this is the best idea. The function time() give you the number of seconds from January 1th, 1970 00:00:00. There's no performance impact because it's only an integer. In MySQL, create a field like that INT, 10, Unsigned.
Time will give you performance on the SELECT and the WHERE. See http://gpshumano.blogs.dri.pt/2009/07/06/mysql-datetime-vs-timestamp-vs-int-performance-and-benchmarking-with-myisam/
The only problem you have is : time is limited to year 2038... but by the time 2038 come, the internal computer clock bytes will be larger ... hope so.
The other thing you may want to worrie about the DATETIME is : PHP time() run under UTC, while DATETIME depend on the timezone...
Stats when you do INSERT with 10000000 rows.
Stats when you SELECT / WHERE with indexes :

What are the pitfalls in the approach to calculate time diff

I wish to calculate the difference b/w 2 times in min:sec format . so is my approch correct
date("i:s",(strtotime($User['end_time']) - strtotime($User['start_time'])));
You may get the problems with timezones on some servers.
A bettter way would be using UTC timezone for calculation:
$date = new DateTime('', new DateTimeZone('UTC'));
$date->setTimestamp(strtotime($User['end_time']) - strtotime($User['start_time']));
echo $date->format('i:s');
Another thing, if they are different in exactly 1 hour, the result will be 00:00
strtotime($User['end_time']) - strtotime($User['start_time']) gives you the difference in seconds. Then you pass it to date, so you get the minute and second of the date whose unix timestamp is that.

generate unix timestamp from last time

suppose i have one column in mysql database which is stated last time one equipment is up in h:i:s format (ex: 00:05:11) or 1d21h, means that the equipment is on since 5 min before, what is the best method i can convert this to unix timestamp, say if using php script how? or direct convert last unix timestamp using mysql function query.
actually, i want to calculate for start time for this equipment uptime in unix timestamp where i have one column in mysql startcapture that will be deducted with last column to get start time. so starttime = startcapture - last (this last time that has to convert to unix timestamp based on now() - h:i:s ). but the problem is sometimes the format change from h:i:s to ex: 1d22h, if h:i:s means the equipment is up since ex: 00:05:11 min before and if 1d22h means the equipment already up 1 day 22 hours before.
so the main things here is to convert last column to appropriate unix timestamp.
please help guys, asap.
update:
simplified like this,
i want to display the start time, that can only be calculate by deducting startcapture column with last column,
startcapture column in unix timestamp, while the last column in h:i:s format sometimes in 1d22h format (means the equipment is already up since 1 day 22 hour or since 00:05:11, hours minute seconds)
the only things I want is to convert last column into unix timestamp which is time() - (00:05:11 or 1d22h),
is there any specific function?
If you have the time parsed into variables then you can use mktime to get the timestamp:
http://www.php.net/manual/en/function.mktime.php

Categories