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');
Related
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.
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.
$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
}
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.
I found this script on php.net and finds the difference between now and a future day. My question is very simple, but is a sample time or how can I make that time that it is needed for the $future_date ? Also what is the purpose of -1970 ?
Also how can I show a message when the future_date is reached or passed?
<?php
function time_difference($endtime){
$days= (date("j",$endtime)-1);
$months =(date("n",$endtime)-1);
$years =(date("Y",$endtime)-1970);
$hours =date("G",$endtime);
$mins =date("i",$endtime);
$secs =date("s",$endtime);
$diff="'day': ".$days.",'month': ".$months.",'year': ".$years.",'hour': ".$hours.",'min': ".$mins.",'sec': ".$secs;
return $diff;
}
$end_time = $future_date - time();
$difference = time_difference($end_time);
echo $difference;
//sample output
'day': 2,'month': 1,'year': 0,'hour': 2,'min': 05,'sec': 41
?>
A unix timestamp checkout the docs for time() and mktime()
You're substracting two values from each other so they need to be compatibable formats to be able to do that. Checking the documentation on time() could have saved you from this question.
date() is also a function you might want to check up on. Using date and the right parameters it will return the current year(Y) month(m) or day of the month(d) you can add and substract to these values and then pass them into mktime to get a unix timestamp like so for the current year in unix timestamp format:
$currentyear = mktime(date(Y));
Below would set $future_date to 1st Dec 2011
$future_date = mktime(0, 0, 0, 12, 1, 2011);
So Hour Min Sec goes:
$future_date = mktime(H, M, S, 12, 1, 2011);
Below would be 13:21:59 1st Dec 2011
$future_date = mktime(13, 21, 59, 12, 1, 2011);
$future_date should be a unix timestamp.
$future_date = strtotime("next week");
To check if the time has been reached
if($future_date <= time()) echo "Date reached";
$future_date would be an integer timestamp (in seconds since Jan 1, 1970) representing some time in the future.
ie:
$nextWeek = time() + (7 * 24 * 60 * 60);
Takes the current date/time and adds 7 days worth of seconds (24 hours, 60 minutes per hour, 60 seconds per minute) to get the integer time of one week from now.
Jan 1, 1970 is significant - it is called the Epoch in UNIX (January 1 1970 00:00:00 GMT) and is often used as a starting point for dates and/or computer "time" (time zero).