date() returns inconsistent time - php

I have the following PHP code on my local apache2 webserver on a Mac Book Pro using PHP 5.3.8 and Default timezone set to Europe/Zurich:
$now = time();
$in5min = $now + 300;
echo "now: " . date('H:m:s', $now);
echo "<br>in 5 min: " . date('H:m:s', $in5min);
echo "<br>now using date only: " . date('H:m:s');
Result looks like this (run at 18:16:12, 26 July 2012):
now: 18:07:01
in 5 min: 18:07:01
now using date only: 18:07:01
Note that I can refresh the page - the seconds change, the hours and minutes remain. So 5 minutes later it's still 18:07.
What's wrong with my webserver settings? And why does the time calculation not work?

The correct format string is i for minutes, not m. m is for months.
date('H:i:s');

You're using the wrong interval, but I would suggest using DateTime::add instead:
<?php
$date = date_create('2000-01-01');
date_add($date, date_interval_create_from_date_string('10 days'));
echo date_format($date, 'Y-m-d');
?>

Haha could hit me in the face: date('H:m:s') is totally wrong. "m" is for month, not minutes. Use date('H:i:s') instead

Related

php timezone not matching with current time on server

i am using following codes to get current time on server as per timezone parameter but i am getting wrong output. About 10/20 minutes delay then server time. And it also not gets updated. How to solve it? any idea?
<?php
function server_time_as_per_users_zone($users_zone){
$dateTime = new DateTime('now', new DateTimeZone($users_zone));
$r = $dateTime->format("d-m-Y h:m A");
return $r;
}
echo server_time_as_per_users_zone("Asia/Dhaka");
You are using a wrong format mask. The m stands for month number and not minutes.
Change it to
function server_time_as_per_users_zone($users_zone){
$dateTime = new DateTime('now', new DateTimeZone($users_zone));
$r = $dateTime->format("d-m-Y h:i A");
return $r;
}
echo 'UTC - ' . server_time_as_per_users_zone("UTC").PHP_EOL;
echo 'Europe/London - ' . server_time_as_per_users_zone("Europe/London").PHP_EOL;
echo 'Asia/Dhaka - ' . server_time_as_per_users_zone("Asia/Dhaka").PHP_EOL;
RESULT:
UTC - 19-04-2017 05:40:23 PM
Europe/London - 19-04-2017 06:40:23 PM
Asia/Dhaka - 19-04-2017 11:40:23 PM

PHP Adding 15 minutes to Time value

I have a form that receives a time value:
$selectedTime = $_REQUEST['time'];
The time is in this format - 9:15:00 - which is 9:15am. I then need to add 15 minutes to this and store that in a separate variable but I'm stumped.
I'm trying to use strtotime without success, e.g.:
$endTime = strtotime("+15 minutes",strtotime($selectedTime)));
but that won't parse.
Your code doesn't work (parse) because you have an extra ) at the end that causes a Parse Error. Count, you have 2 ( and 3 ). It would work fine if you fix that, but strtotime() returns a timestamp, so to get a human readable time use date().
$selectedTime = "9:15:00";
$endTime = strtotime("+15 minutes", strtotime($selectedTime));
echo date('h:i:s', $endTime);
Get an editor that will syntax highlight and show unmatched parentheses, braces, etc.
To just do straight time without any TZ or DST and add 15 minutes (read zerkms comment):
$endTime = strtotime($selectedTime) + 900; //900 = 15 min X 60 sec
Still, the ) is the main issue here.
Though you can do this through PHP's time functions, let me introduce you to PHP's DateTime class, which along with it's related classes, really should be in any PHP developer's toolkit.
// note this will set to today's current date since you are not specifying it in your passed parameter. This probably doesn't matter if you are just going to add time to it.
$datetime = DateTime::createFromFormat('g:i:s', $selectedTime);
$datetime->modify('+15 minutes');
echo $datetime->format('g:i:s');
Note that if what you are looking to do is basically provide a 12 or 24 hours clock functionality to which you can add/subtract time and don't actually care about the date, so you want to eliminate possible problems around daylights saving times changes an such I would recommend one of the following formats:
!g:i:s 12-hour format without leading zeroes on hour
!G:i:s 12-hour format with leading zeroes
Note the ! item in format. This would set date component to first day in Linux epoch (1-1-1970)
strtotime returns the current timestamp and date is to format timestamp
$date=strtotime(date("h:i:sa"))+900;//15*60=900 seconds
$date=date("h:i:sa",$date);
This will add 15 mins to the current time
To expand on previous answers, a function to do this could work like this (changing the time and interval formats however you like them according to this for function.date, and this for DateInterval):
(I've also written an alternate form of the below function here.)
// Return adjusted time.
function addMinutesToTime( $time, $plusMinutes ) {
$time = DateTime::createFromFormat( 'g:i:s', $time );
$time->add( new DateInterval( 'PT' . ( (integer) $plusMinutes ) . 'M' ) );
$newTime = $time->format( 'g:i:s' );
return $newTime;
}
$adjustedTime = addMinutesToTime( '9:15:00', 15 );
echo '<h1>Adjusted Time: ' . $adjustedTime . '</h1>' . PHP_EOL . PHP_EOL;
get After 20min time and date
function add_time($time,$plusMinutes){
$endTime = strtotime("+{$plusMinutes} minutes", strtotime($time));
return date('h:i:s', $endTime);
}
20 min ago Date and time
date_default_timezone_set("Asia/Kolkata");
echo add_time(date("Y-m-d h:i:sa"),20);
In one line
$date = date('h:i:s',strtotime("+10 minutes"));
You can use below code also.It quite simple.
$selectedTime = "9:15:00";
echo date('h:i:s',strtotime($selectedTime . ' +15 minutes'));
Current date and time
$current_date_time = date('Y-m-d H:i:s');
15 min ago Date and time
$newTime = date("Y-m-d H:i:s",strtotime("+15 minutes", strtotime($current_date)));
Quite easy
$timestring = '09:15:00';
echo date('h:i:s', strtotime($timestring) + (15 * 60));

Server Time - driving me nuts :-)

Okay so I have a server in Denver with a user in New Zealand. I know everything about the user (timezone etc) and through the program they request something to happen in advance - let's say at 11:30am on August 5th 2013. I have a CRON job that runs every 15 minutes and asks the database if any requests are pending for the next 15 minute period, but how do I convert their stored time to the servers equivalent.
I set the default timezone for calculations: date_default_timezone_set('America/Denver')
I take the time now on the server and turn it into epoch: strtotime(date('Y-m-d H:i:s'))
I add the 15 minutes to create a range: $forward15 = strtotime('now +15 minutes')
I get the user chosen date from the database (and their timezone): 2013-08-05 11:30:00
Now what? If I convert that into epoch, it'll just be the servers version of that date.
NEW SOLUTION SEE BELOW!
If you know the timezone you can simply "add" it to your time.
For example:
server time: 01/01/01 00:00
the time the user wants: 01/01/01 01:00
the timezone of the user: GMT - 5
Just get the time (01/01/01 01:00) and add +5 => 01/01/01 06:00
So: your script needs to be executed at 01/01/01 06:00
(convert to timestamp where needed)
Added a little php to demonstrate
<?php
$servertime = time(); //timestamp of 01/01/01 00:00
$usertime = "01/01/01 06:00";//database
$userUTC = "-5";//database
strreplace($userUTC, "-", "+";
$replacetext = $userUTC . " days";
$usertime = strtotime($replacetext, $usertime);//now usertime is in your local timezone
$crontime = date("d/m/Y H:i");//the time you want the script to be executed
?>
I'm just assuming that the timezone is saved as "-5" for example and not Europe/Amsterdam Just tell me if i'm wrong.
edit 14:37
This could be a even better solution i think!
<?php
$usertime = "01/01/01 06:00";
$userUTC = "-5";
$userdate = $usertime . " " . $userUTC;
$usertimestamp = strtotime($userdate);//now you have the timestamp with correct timezone
$crontime = date("d/m/Y H:i", $usertimestamp);//formatted to the right date
echo $crontime;
?>
Edit: 25-07-2013 14:26
New solution to suit your database:
<?php
$usertime = "01/01/01 06:00";
$userUTC = "Pacific/Auckland";//get from database
$userdate = $usertime . " " . $userUTC;
$usertimestamp = strtotime($userdate);//now you have the timestamp with correct timezone
$crontime = date("d/m/Y H:i", $usertimestamp);//formatted to the right date
echo $crontime;
?>
the server is in which GMT time zone here is extremely easy way to get time and date for any time zone. This is done with time() and gmdate() function. gmdate() function normally give us GMT time but by doing a trick with time() function we can get GMT+N or GMT-N means we can get time for any GMT time zone.
For example you have to get time for GMT+5 we will do it like following
<?php
$offset=5*60*60; //converting 5 hours to seconds.
$dateFormat="d-m-Y H:i";
$timeNdate=gmdate($dateFormat, time()+$offset);
?>
Now if you have to get the time which is GMT-5 now we will just subtract the offset from the time() instead of adding into time like in following example we are getting time for GMT-4
<?php
$offset=4*60*60; //converting 5 hours to seconds.
$dateFormat="d-m-Y H:i";
$timeNdate=gmdate($dateFormat, time()-$offset);
?>

subtract time away from a php date time

I have a report I built but the problem is the datetimes in the database for the 3 major events are the same as the system processes then so fast, there is no easy way about it as I aggregate data from 4 servers into one jquery datatable and sort by date time decending.
So my question is how can I take a variable in PHP (string of mysql format date time), and reduce it by 1 second?
dognose answer is fine. Find below a method using DateTime.
For those who are not too confident about strtotime :-)
$string = "2013-06-26 18:00:00";
$date = DateTime::createFromFormat('Y-m-d H:i:s', $string);
$date->sub(new DateInterval('PT1S'));//substract 1 sec
echo $date->format('Y-m-d H:i:s'); //print : 2013-06-26 17:59:59
Doc about "PT1S" here (this can be read as Period Time 1 second)
use date along with strtotime should do the trick:
http://php.net/manual/de/function.strtotime.php
$string = "2013-06-26 18:00:00"; //can have any (valid) format
$subSeconds = 1;
$date = date("Y-m-d H:i:s", strtotime($string . " - {$subSeconds} second"));
echo $date."<br />"; //2013-06-26 17:59:59

Behavior of <?php echo date("H:i:s", 0);?>

I used :
<?php
echo date("H:i:s", $var_time_diff);
?>
to construct a time between two dates.. and in my head it was
$var_time_diff = 36000 = display 10:00:00 for 10 hours.
But in fact
<?php echo date("H:i:s", 0);?>
display 01:00:00 and not 00:00:00.
So we have
$date_a = "18:15:04";
$date_b = "23:15:04";
$diff = strtotime($date_b) - strtotime($date_a);
All is ok for the moment $diff is 5 hours but if we display date like this:
echo date("H:i:s", $diff);
it will be "06:00:00".
So something wrong with my php config or it's a normal behavior for php function date?
The date() function uses your current time zone. If you want to ignore your configured time zone, use date_default_timezone_set() or use gmdate().
You're in some timezone other than UTC. Try:
<?php
date_default_timezone_set('UTC');
echo date("H:i:s",0) . "\n";
I'm not sure why, but date outputs the current hour in your example, make a timestamp from your seconds first and it works. I'll be following this question for a deeper explanation though.
$date_a = "18:15:04";
$date_b = "23:15:04";
$diff = strtotime($date_b) - strtotime($date_a);
echo date("H:i:s", mktime(0,0,$diff));
edit: ah, so it adjusts to your current timezone, so does mktime, so the effect is negated.
use mktime, and min 1 for hour # $diff

Categories