How to get delay of 30 sec in current time in php? - php

Date("F d Y H:i:s") gives current time in php. How can I get delay of 30 secs and 1 min in current time using php, not sure but something like Date ("F d Y H:i:s-30") or Date("F d Y H:i-1:s") ?

date("F d Y H:i:s", time() + 90)
date has a second, optional parameter: the timestamp to convert to a string. time returns the current timestamp, which is just an integer, so you can add 90 to it to get the time 90 seconds from the current time.

You mean format the time for 1:30 in the future?
date('format string', time()+90);
In the above, 90 can be any positive or negative offset.

In PHP, dates and times are generally worked with as timestamps ; which are a number of seconds since 1970 or so.
To get the current timestamp, you can use time()
And to format it, you use date(), as you already know.
So, for current time plus 1 min and 30 seconds (ie, 90 seconds) :
date('F d Y H:i:s', time() + 90);
Or, as an alternate way (a bit more fun ? ), you can use strtotime() :
echo date('F d Y H:i:s', strtotime('+1 minute +30 seconds'));
OK, more complex in this case, I admit ^^
But, in some other situations, it's good to know it exists ;-)

Use this function:
date("F d Y H:i:s",time()-90)

Related

How to deduct time in php 24 hours format

How to subtract 00:00:00 example with 1 hour?
I've tried:
$minus1=strtotime(00:00:00)-strtotime('-1 hour');
but instead of an answer of 23:00:00, I got -1502671524 instead.
You can use DateTime and DateInterval option to do in PHP.
$date = new DateTime('00:00:00');
$date->sub(new DateInterval('PT1H00M'));
echo $date->format('H:i:s') . "\n";
The P stands for Period. The T stands for Timespan. H stands for Hour to reduce and finally M stands for Minutes to reduce.
See DateTime, DateTime::sub, and DateInterval in the PHP manual. You'll have to set the DateTime to the appropriate date and time, of course.

PHP get 00:00:00 of a unix timestamp

If I got a unix time which is e.g
1407050129
How do I get the 12:00AM of that unix day in unix timestamp , means the first minute of the day of that unix time.
Example if i want get today first minute
$today_first_min = strtotime("00:00:00");
Try this:
$that_day = "1407050129";
$that_day_first_min = strtotime(date('Y-m-d', $that_day) . ' midnight');
See demo
An alternate method to arrive at the same result... Unix time is a count of seconds since 1970-01-01 00:00:00 UTC, so each whole day is a multiple of (60*60*24) seconds.
Using this fact you can use the modulus operator (%) to calculate and then remove the remainder (ie. the seconds, hours and minutes) from the day, and get back to the first hours!
date_default_timezone_set('UTC');
$that_date = 1407050129;
$first_hour = $that_date - ($that_date % (60*60*24));
print date('Y-m-d H:i:s', strval($first_hour));
// 2014-08-03 00:00:00

How do I convert this timestamp to PHP? Bing Maps API

I've been playing around with the Bing API using json and PHP. The array spits out the following for dates:
[end] => /Date(1354867200000)/
[lastModified] => /Date(1349441488000)/
I thought this was a unix timestamp, but it I don't think it is. What I did was a preg_replace like this
$last_updated = $resource->lastModified;
$last_updated_timestamp = preg_replace('/[^0-9.]*/','',$last_updated);
Then tried to convert it to a date
$last_updated_date = date('l F d Y g:i:s A',$last_updated_timestamp);
The results that it's showing me for date range back from the year 1967 to 2000. Is this a different kind of timestamp that I don't know of? If so, how do I correct this? Any help would be appreciated!
The number part is milliseconds-since-the-Epoch (January 1, 1970 at midnight — the milliseconds version of a unix timestamp). This is a fairly conventional way to represent dates in JSON (since JSON doesn't have a date type).
So getdate(theNumber / 1000) will give you the date (since getdate expects seconds, not milliseconds, since The Epoch).
If what you want to do is convert a unix timestamp to date format, you can do it by doing the following:
date("F j, Y g:i a", strtotime($unix_timestamp));
Where $unix_timestamp is your unix timestamp in this case.
You can always print it out for testing purposes by adding echo before it.
So in this case it could be:
$last_updated_date = date("F j, Y g:i a", strtotime($last_updated_timestamp));

Why can't I change the time for a date for less than ~29 minutes and why is the PHP time() and date() combo return a time that's ~27 minutes off?

So I have a block of PHP code, and I simply wanted to have a UNIX timestamp for the current date, and one that is 25 minutes earlier. The UNIX timestamp changes accordingly, but when I use each timestamp and convert it to a formatted date with the date('M d, Y A -- h:m:s',$current) or date('M d, Y A -- h:m:s',$old), both times turn out exactly the same. It seems a change greater than 29 minutes works, but I'm not sure why. And the second part of the question: with using time() and date() and even setting the timezone to my own, the time it returns is about 20-30 minutes behind, and this also concerns me.
<?
date_default_timezone_set('MST');
$current = time();
$old = time() - (25 * 60);
echo $current . ' - ' . $old; // Prints 1330473445 - 1330471945
echo date('h:m:s A -- M d, Y',$current); // 04:02:25 PM -- Feb 28, 2012
echo date('h:m:s A -- M d, Y', $old); // 04:02:25 PM -- Feb 28, 2012
?>
This is how it prints on my screen. Different UNIX timestamps, but same formatted date. And I suppose you mean system clock as in the one I need to edit via BIOS. As far as the clock on my computer is concerned, that's what I was comparing it to.
Update
Solved. Used an 'm' for seconds rather than the 'i'
The reason is that you used "h:m:s"
echo date("M d, Y A -- h:i:s",$ut); and your problem is solved
m is not "minutes" m = month
i = minutes
I know it's evil, I just fell for it myself for a while ;)
Here the docs: http://www.php.net/manual/de/function.date.php
what does this give you:
<?php
echo $current.' - '.$old;
?>
you can also use strtotime('-25 minutes') as your second argument.
Also, I think you mean i not m for minutes. This is using month not minutes. Try with i instead and see if that works?

What is this time format?

I am being returned this time format from an API:
1287498792000
Can anyone advise what format that is and how I would parse it in PHP?
This format is the Number of milliseconds since 1970-01-01.
Your date represents 2010-10-19 # 14h33 if i'm not mistaken.
Just divide it by 1000 and use the standard php functions for unix timestamps like date to display it or getdate to extract the different parts.
It's a Unix timestamp represented in milliseconds — equivalent to the return value from time() multiplied by 1,000 (timestamp in PHP are in seconds, not milliseconds).
You can use it directly1 in PHP, e.g. for the date() function:
print date('l jS \of F Y h:i:s A', 1287498792000 / 1000);
// Outputs: Tuesday 19th of October 2010 02:33:12 PM
EDIT
1 Yes, it seems to be in milliseconds. Divide by 1,000 in order to get a timestamp that PHP understands.
It's a UNIX timestamp - it represents the count of seconds since January 1st, 1970.
You can use PHP's date() function to convert it to a human readable format.

Categories