How to convert date to hours - php

I am having :
hours1 : 403440
and date2 as :2016/01/10
I need to convert date2 in hours and find the difference between the two which should again be in hours.

The second parameter of date is a unix timestamp which is in seconds so multiple your hours by 3600 (3600 seconds per hour).
$hours1 = 403440 * 3600;
$date1 = date("d-m-Y H:i:s", $hours1);
echo $date1;
Output:
09-01-2016 19:00:00
Per your update your code should be:
$date2 = strtotime('2016/01/10');//get date to seconds from 1970
$hours1 = 403440 * 3600; // convert from hours to seconds
echo ($date2 - $hours1)/3600;//subtract seconds then divide by 3600 to get how many hours difference is
Output:
5

The second parameter of date() is unix timestamp which is the number of seconds since January 1 1970 00:00:00 UTC.
So, if $hours1 is number of hours since January 1 1970 00:00:00 UTC, it should be
date("d-m-Y H:i:s",$hours1*3600);
to yield the correct date string.
On the other hand, $hours2=(strtotime($date2)/3600); is giving you the correct number of hours since January 1 1970 00:00:00 UTC.

Related

How to check how many days passed from timestamp - counting the days at midnight

I'm trying to know how many days have passed from a certain timestamp, but the problem is I can't set it up, so that after midnight will count it as another day.
Here is what I tried:
<?php
$now = time(); // or your date as well
$your_date = 1572123244;
$datediff = $now - $your_date;
echo round($datediff / (60 * 60 * 24));
If I put a timestamp of five minutes before midnight (1572134100), five minutes after midnight should appear that "one day passed"
The usual way of counting the days passed since a given timestamp would be something like this:
$dt = date_create_from_format('U', 1572046200);
$diff = $dt->diff(new DateTime());
echo $diff->days;
But this counts the full 24 hour periods as days. In your case you want to count calendar dates irrespective of the time of day. I would recommend then to ceil the timestamp to the midnight.
$dt = date_create_from_format('U', 1572047700);
$dt->setTime(0, 0); // set time to 00:00
$now = new DateTime('now', new DateTimeZone('UTC')); // time now, but in UTC
$now->setTime(0, 0); // set time to 00:00
$diff = $dt->diff($now);
echo $diff->days;
I am not sure about your current time zone, but timestamps are by nature in UTC, hence you should probably normalize your local time to UTC as well.
What this code does is it sets both today's date and the timestamp you are comparing against to the midnight of the UTC day and then calculates the difference between the two. Taking the time out of equation, this will always count the full 24 hour days.

if date time difference is over cetain amount

$now = new DateTime();
$future_date = new DateTime($date);
$interval = $future_date->diff($now);
echo $interval->format("%d days, %h hours, %i minutes, %s seconds");
Basically I want to use the difference between the two dates now and whatever the person has booked. I need to use an if statement to check if the difference is between 24 hours / 1 day.
how do i use datetime in if statements?
like
If ($interval > 24 hours) {
allow } my problem is how do i write 24 hours in php? sounds really dumb I know.
$now_ts = $now->getTime();
$future_date_ts = $future_date->getTime();
if ($future_date_ts - $now > 60 * 60 * 24) {
// more than 24 hours before $future_date
}
If you also want the diff the other way add:
$now - $future_date_ts > 60 * 60 * 24 // more than 24 hours after $future_date
This gives you a 48 hour range around $future_date.
You could instead use the UNIX timestamp ( the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).
time(); will return the current Unix timestamp and you can convert your future_date to a timestamp using strtotime();
So you can calculate the difference in seconds using:
$diff_secs = strtotime($future_date) - time();
And knowing 1 day is 24 hours which is 24*60 minutes which is 24*60*60 seconds,
$diff_days = (strtotime($future_date) - time())/(24*60*60);
Or you could use the difference in hours as your comparison :
$diff_hours = (strtotime($future_date) - time())/(60*60);
if($diff_hours > 24) {
//do something if its more than 24 hours away
}

Get timestamp for date to 60 days

I have this date for timestamp like 1393516517, but I need the timestamp for 60 days.
I tried this way:
( 24 * 60 * 60 ) * 60 = 5184000;
Is this the correct way to do it?
1393516517 is called a UNIX timestamp and is the number of seconds since the Unix Epoch (January 1, 1970 00:00:00 GMT). This timestamp in particular corresponds to 02/27/2014 3:55 PM GMT.
If you want to add 60 days to a UNIX timestamp, then you do indeed need to add the number of seconds 60 days equals just as you do. So 1393516517 + 5184000 = 1398700517 which is 04/28/2014 3:55 PM GMT.
Assuming you are trying to get a Unix timeatamp for a date 60 days into the future of your start date:
$date = new DateTime('#1393516517');
$date->modify('+60 days');
echo $date->format('U');

PHP - gmdate() and Large Integers

I'm getting a result I'm not expecting from gmdate()
<?php
$secs = 175707;
echo gmdate("H:i:s", $secs); // result: 00:48:27
?>
The result is 00:48:27, which is way off. It appears the hours is getting pushed down a position. Am I suspecting that right?
gmdate works on dates, not periods of time. In other words, your timestamp is being interpreted as a point in time early in January 3rd of 1970 (specifically 00:00:00 1 Jan 1970 + 15707 seconds = 00:48:27 3 Jan 1970). This is where your 00:48:27 comes from.
gmdate (and date) are not meant to be used this way. If you just want to calculate hours/minutes/seconds based on number of seconds, calculate them directly:
$seconds = 175707;
$hours = floor($seconds / 3600);
$seconds -= $hours * 3600;
$minutes = floor($seconds / 60);
$seconds -= $minutes * 60;
echo "$hours:$minutes:$seconds"; //48:48:27
Make note that this does not work with civil days. This is because of daylight saving time. A day is not always 24 hours. Sometimes it's 23, and sometimes its 25 when DST is coming into effect or ending. With days as a unit of measure (i.e. a day is always 24 hours), this does work. As an example, 10 March is 23 hours and 3 Nov is 25 hours in the United States. If you are happy with static 24 hours days, then the same approach will of course work.
gmdate() expects a UNIX timestamp - seconds since Jan 1/1970. You've passed in the equivalent of Saturday, Jan 3rd, 1970, 12:48:27am.
e.g. try this:
echo gmdate('r', 175707);
From the manual
the time returned is Greenwich Mean Time (GMT)
So if you're in CET (GMT +1) it indeed will give you an hour of 0.
Have a look at this if you want to see where you are going wrong:
echo date("d-M-Y H:i:s", 175707);
That will give an output of 03-Jan-1970 00:48:27. i.e. 175707 seconds from 00:00:00 on the 1st of January 1970 = 03-Jan-1970 00:48:27.

How to get rough age of a timestamp/datetime object?

In PHP, if I have a datetime object or timestamp as properties of an object, how can I get a rough age for the record in PHP.
So that instead of a formated date string it says something like"about 1 hour ago", "3 days ago", "1 year ago" etc.A bit like redmine has for task comments, for example.
What is this process called?I tried searching for age and datetime and keep finding code to get someone's age of birth, which is not what I'm looking for.
if you have 2 objects. You need to convert first object to timestamp and second object is time();
time() - Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).
Then you substract time() with date you want and you get difference in seconds. To make minutes from seconds you need divide it by 60, to make hours you need to divide it by 60 * 60 = 3600 etc.
$secondsAgo = time() - $timeStamp;
$minutesAgo= (time() - $timeStamp) / 60;
$hoursAgo= (time() - $timeStamp) / 60*60;
$daysAgo= (time() - $timeStamp) / 60*60*24;
To change string to timestamp you can use strtotime()
The other solution is to use diff() on DateTime object.
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
This returns DateInterval object which has a lot of properties that are public
y Number of years.
m Number of months.
d Number of days.
h Number of hours.
i Number of minutes.
s Number of seconds.

Categories