How to get strtotime() with written time format? - php

I need to input, 09 hours 20 minutes to strtotime and to get the corresponding timestamp. I tried out, strtotime("09 hours 20 minutes",0) but it gives me time passed in seconds in current day only. ie 33600.
I need to get the exact timestamp. ie time passed in seconds from 1-1-1970 to current day 09:20. Is there anyway? Any help will be appreciated.

Like Wong said in the first comment:
use "today" as offset
strtotime("09 hours 20 minutes", strtotime("today"));

Related

PHP timer based on date

Lets say I have a date and time in the following format:
09:24:24 Mar 07, 2014 PST
Using PHP I want to be able to work out what the date and time will be exactly 3 days from that date. Then use that info to display a count down, for example: Time up in 2 days, 3 hours, 27 mins and 5 secs.
And finally display a message when the time is up.
I have no idea how to approach this, can anyone point my in the right direction?
Thank you.
Well there's certainly plenty of ways to approach this, and you haven't posted any code of anything that you've already tried, so the best I can do is point you in the right direction.
I'm assuming you're getting this date from the client (09:24:24 Mar 07, 2014 PST) as a string passed to your PHP script:
The first thing you need to do is parse the string so you can pull out the year, month, day, hour, minutes and seconds. So that you can create a DateTime object in PHP with:
$date = new DateTime();
$date->setDate($year, $month, $day);
$date->setTime($hour, $minute, $second);
Then you can use add() to add 3 days like so:
$date->add(new DateInterval("P3D"));
Now you have your date that you can pass back to your client (javascript) to display some sort of a countdown timer (search google for this, you'll get a plethora of results).
$echo $date->format("Y-m-d H:i:s");

Comparing and checking PHP Date()

I'm trying to figure out how to compare against date(). I'm following along in a tutorial about how to use this function to compare the current time against the time a cache file was last modified. In the tutorial, the author uses "10800" as 3 hours and the code looks something like:
(filemtime($cache) < (time()-10800))
I have no problem understanding how this comparison works but I just don't get how the the expression of time, "10800", is formatted.
Just for the record I spent a solid 15 minutes looking for an answer so I'm not just being ignorant of Google haha.
Thanks!
10800 is in seconds..
all unix timestamps are measured in seconds since the epoch... 1 being the first second of 1970.
This explains why when you have a bad strtotime value and you are interpreting it with date i.e.
date(strtotime("last tomorrowday"));
it ends up showing you 1969-12-31 ... strtotime is returning 0 and if 1 is the first second of 1970 then 0 will be interpreted as the last second of 1969
It's in seconds,
3 hours = 3 * 60 * 60 = 10800 seconds
As time function returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT). You neeed to subtract 10800 from it to get timestamp of time before 3 hours.

PHP - time minus time to minutes

In php i have two times - 11:00:00 and 12:45:00. I want to get the difference between them in minutes, in this case 105 minutes. Whats the best way that can be done?
Thank you!
Here you go:
( strtotime('12:45:00') - strtotime('11:00:00') ) / 60
strtotime() is a very useful function. It returns the Unix timestamp for a wide variety of times and dates. So, if you take the two timestamps, and subtract them, then you have the difference in seconds. Divide by 60 to get the minutes.
$time_diff = strtotime('2013-03-13 12:45:00') - strtotime('2013-03-13 11:00:00');
echo $time_diff/60;
I just kept dates as not sure if I keep the time part only it would return the correct diff or not.
EDIT
I just tested it works without date too ...
$time_diff = strtotime('12:45:00') - strtotime('11:00:00');
echo $time_diff/60;
So to answer you question - strtotime() returns a timestamp (the number of seconds since January 1 1970 00:00:00 UTC) so you simply divide it by 60 to convert it result into minutes.

Error on strtotime()

$dateTime="2011-10-12 00:00:00";
echo $newDateTime =date("Y-m-d H:i:s", strtotime($dateTime.' -1 hours 30 minutes'));
The result of above code is '2011-10-11 23:30:00'. However, the correct answer should be
2011-10-11 22:30:00.
Is there anying wrong in the code and can anyone help me?
Many thanks
23:30 is the expected result (once you know what is happening).
The relative parts of the string (-1 hours 30 minutes) are processed separately as -1 hours and 30 minutes. They are two instances of the number space? (unit | 'week') format as described in the Relative Formats documentation.
Because of this the cumulative relative change in the time is only -30 minutes, which from midnight gives 23:30.
To get the effect that you desire, either:
use a single relative statement (e.g. -90 minutes)
make your original minutes statement negative as -1 hours -30 minutes
or, use the special ago format as 1 hours 30 minutes ago
See http://php.net/datetime.formats.relative for more details.
date functions aren't fully daylight savings aware. Try using dateTime objects instead

Trouble getting age in hours

I am trying to calculate the age of something in hours.
$data['record'] is a mysql NOW() timestamp in a DATETIME field.
$data['record'] is 20 minutes old, when I do :
$minutes= date('i',(strtotime("now")-strtotime($data['record'])));
$minutes returns 20 properly, however for some reason when I try $hours it returns '5'.
$hours = date('g',(strtotime("now")-strtotime($data['record'])));
This does not make sense, as $hours should be returning 0 as the record is less than 60 minutes old...
When I checked the value of "strtotime("now")-strtotime($data['record'])" it is equal to '980'. Help!
Please compare the output of strtotime("now") of php and select now(); in sql. I think there is a timezone problem hidden here.
As you said, strtotime("now")-strtotime($data['record']) returns 980, which should be in minutes. 960 is divideable by 60 and comes out at 16 hours, so 980 is 16 hours 20 minutes - the 20 minutes are exactly what you are looking for. You'll need to adjust either instance to use the time of the other - I would go with always using UTC. If you need to display it, parse it appropiately and output the local time.
Please See: http://php.net/manual/en/function.date.php
When the $format parameter="g", it returns a value 1-12.
Date will not quite work like you're expecting it to.
Date takes a time stamp (# of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)), and converts that into a legible time format. Essentially, with a value of 980, you are going to get January First at midnight + 980 seconds (roughly January 1 1970 00:16:20 GMT. When you convert for the time zone difference, (chances are, about 5 hours difference) that's how you get five.
To fix this, simply take 980, and divide by 60 to get minutes, then divide by 60 again to get hours, so:
$hours = ((strtotime("now")-strtotime($data['record'])) / 60) / 60;
There's no need for date, as you need a relative time, not an absolute time.

Categories