I have this code to display a date and time from the database. How would I modify it to subtract 6 hours from the timestamp.
date('m-d g:Ga', strtotime($row['time_stamp']))
UNIX time stamps are seconds since the Epoch. Simply subtract the number of seconds in six hours:
date('m-d g:Ga', strtotime($row['time_stamp'])-21600)
You could alternatively use strtotime again:
date('m-d g:Ga', strtotime('-6 hours', strtotime($row['time_stamp'])))
Since it's in MySQL, you can have MySQL do the calculation for you:
SELECT DATE_FORMAT('...', DATE_SUB(time_stamp, INTERVAL 6 HOUR)) ...
This would save you the overhead of MySQL having to conver its internal representation into a full string, and the overhead of PHP's strtotime() parseing that string just to turn it back into yet another string. While strtotime is magical sometimes, it's definitely not efficient.
$vindate=date('H:i:s', strtotime($row['ms_attend_time'])+19800);
echo $vindate;
Related
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.
Suppose that I have datetime like 2013-01-16-0900. How can I subtract a length of time - say, 30 minutes?
This will subtract 30 minutes from the supplied timestamp.
echo date( 'Y-m-d-Hi', strtotime( $timestamp ) - 30 * 60 );
$timestamp = '2013-01-16-0900' in this example.
Is it stored as a string exactly like you posted, or is it actually a unix timestamp? If it's a string you can run strtotime on it then subtract 1800 (seconds) from it, and then convert it back with date(). That would probably be the easiest way.
I have a strong preference towards using PHP's built in DateTime Module and adding or subtracting time using PHP's built in DateInterval Module. This allows you to perform reasonably complex date/time math and still respect the rules of time.
Also of note is DateTimeZone
Example Time
$dt = new DateTime("2013-01-16-0900", new DateTimeZone("America/Los_Angeles"));
$dt->setTimeZone("America/New_York");
$dt->sub(new DateInterval("PT30M"));
print $dt->format('Y-m-d H:i:s');
There is a bit of a learning curve to assembling time interval strings but once you figure it out, you should be able to write a re-usable function.
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.
Basically, I have a time stamp that looks like 2010-12-15 16:14:06. If the time stamp is seconds old I want it transform it into "N Seconds Old." If the time stamp is minutes old, I want it to transform it into "N Minutes Old." etc. Is there any built in PHP functions that will do this?
if you're running 5.3 you can use DateTime::diff to do it. It returns a DateInterval object which contains the info you'll want.
As mentioned by others, you need to have some "starting point" to measure against. There are a number of ways to turn a time stamp such as 2010-12-15 16:14:06 into a PHP timestamp (seconds since 1970-01-01 00:00:00). If that's your "starting point", simply subtract it from the current time or whatever to get the difference in seconds. If less than a certain amount (say, 60), give that as "N seconds ago". If greater, divide by 60 to get minutes ago, or 3600 to get hours ago, etc.).
Hi All
im trying to build a simple form that the user use it to enter his leave request.
the form contains from time input field and to time input field,and these two filds will contains values in this syntax:
from time: 15:59 pm
to time: 16:59 pm
i have two questions:
1. what is the Datatype that i should use to store p.m and am in the record in mysql database, and not only the time?(i try to use Time,DateTime Date) but these datatypes only stores the time without p.m,a.m
2. what is the best way to calculate the diffrence between these two times?
Thank You
If you are comparing dates/times I find it easiest to work with a timestamp.
There are tons of date functions in PHP if it's just to output the date in the format you want have a look at date functions
The best way to store times in the database is in 24-hour time and use PHP's date function to format the time when you pull it to display it using AM or PM.
To compare two times, I suggest looking at thestrtotime function. This will return the time in UNIX fashion (seconds). You can then compare which time is greater or less than each other, or even perform basic mathematics operations on them (like determining the amount of seconds between each time and then dividing by 60 to determine minutes, etc).
Just store it as a timestamp, you can add pm/am automatically when you output:
print date('G:i a',1294239540); // prints 15:59 pm
and to get the difference in seconds just use strtotime and substract:
print (strtotime('16:59') - strtotime('15:59')); //prints 3600 (1 hour in seconds)
1) This doesn't make any sense - the database stores a representation of an exact instant in time, not an ambiguous 12 hour format with no am/pm. Regardless of how the db is storing it, you can just calculate the am or pm:
<?php
$am_pm = date('a', $timestamp);
?>
or even better, in mysql using your time field:
SELECT DATE_FORMAT(date_field, '%p')
2) For this you can use either php's date_diff, or in mysql:
SELECT DATEDIFF(date_one, date_two)
question 1
stored as time, which use ISO 8601 (hh:mm:ss)
question 2
use timediff, like
select timediff(cast('13:59:29' as time), cast('10:20:00' as time));
>> 03:39:29
To display the AM/PM
select time_format(cast('13:59:29' as time), '%r');
Storing am and pm when you're using 24 format is redundant. Anything between 11:59 and 00:00 is automatically pm. DateTime/timestamp should be more than sufficient to do what you're trying to accomplish. Then you can convert it using strftime back to 12-hr with am/pm.