1 hour skipped when i add seconds manually - php

I know it's gonna sound you strange.. but it's happening..
I am trying mktime() function to create a seconds string:
$time = mktime(21,0,0,3,29,2014);
echo date("d-M, h:i A", $time);
And then I add 21600 (6 Hours) seconds in it..
$newstr = $time+21600;
echo echo date("d-M, h:i A", $newstr);
I Expect this output:
29-Mar, 09:03 PM
30-Mar, 03:03 AM
But I am getting this:
29-Mar, 09:03 PM
30-Mar, 04:03 AM // It must be 03:03 AM
Any one knows what the problem is..? I am using xampp.

Your timezone is set to a region for which Daylight Savings Time is enacted on March 30, 2014, so the latter date ends up being adjusted to DST and is one hour later than you'd expect mathematically.
http://www.timeanddate.com/news/time/europe-starts-dst-2014.html
You can verify this by printing your before and after dates with the timezone marker e and DST marker I included in date's mask.

Maybe you can try to add this line before your code
ini_set('date.timezone','UTC');

Related

I have a specific value and i want to strtotime

I want to add a minute to the time which is the post posted
Let say that $time_posted = "12:14" where 12 is hours and 14 minutes, what i actualy want is to add 1 minute to the $time_posted
NOTE: $time_posted is for different posts different.
Thanks in advance.
echo date("H:i", strtotime("12:14") + 60);
Change the H depending on what you need (look at the values for that here or below). I chose H because I assumed it's a 24-hour clock with leading zeros, but you may change it to:
g: 12-hour format of an hour without leading zeros 1 through 12
G: 24-hour format of an hour without leading zeros 0 through 23
h: 12-hour format of an hour with leading zeros 01 through 12
H: 24-hour format of an hour with leading zeros 00 through 23
The 60 indicates 60 seconds (or 1 minute).
You can use PHP's built-in function mktime for this. With this, you can add or subtract from any part of a date by just using a plus (or minus) sign after the part you want to change. Here's an example of adding 1 to the minute part of a time:
$time_posted = '12:14';
// SPLIT APART THE HOUR AND MINUTE
list($hour, $minute) = explode(':', $time_posted);
$new_date = date("H:i", mktime($hour, $minute + 1, 0, date("m"), date("d"), date("Y")));
print $new_date;
This will output: 12:15
Here is a working demo
Edit:
I just saw that the time format can be in different formats. I don't really know what to tell you there other than you need to find a way to normalize the data. You should never rely on users to input whatever they want. Obviously this code will not work if it can't parse out hour and minute from the timestamp. You'd need to write a complex REGEX to search for all possible combinations of user-supplied combinations. Not something you want to do normally.

date() with Unix time.

I'm having trouble with filtering strtotime('Monday this week', time()
Beginning with the basics.
I use the following converter: http://www.onlineconversion.com/unix_time.htm
I use to display the time: date('d/m/Y h:i:s', $timestamp);
1400630400 --- 20/05/2014 24:00 --- in php (21/05/2014 12:00:00)
1400544000 --- 20/05/2014 00:00 --- in php (20/05/2014 12:00:00)
Ok, displays time date +12 hours, but...
1400616000 --- 20/05/2014 20:00 --- in php **(20**/05/2014 08:00:00)
Why is this? Should not it be 21/05/2014 08:00:00?
This is because date('h'); only displays 0-12 hour format.
In other words, no real differentiation between AM and PM. If you want 24 hour date format, you want to use date('H');, 'H' gives you hours leading from 0 through to 24.
See the PHP Manual for more date(); formats :)
EDIT
PHP date manual: http://php.net/manual/en/function.date.php
it might be about timezones: https://php.net/manual/en/function.date-default-timezone-set.php
as #Zander Rootman said, it's about format because 24:00 and 12:00 are the same hour, depending on format

Server Date resolving issue

$eve['start']['dateTime'] = 2013-05-02T14:00:00+05:30;
$current_date = date("m-d-Y",strtotime($eve['start']['dateTime']));
$start_time = date("H:i A",strtotime($eve['start']['dateTime']));
when i use the above code am getting it as 05-02-2013 08:30 AM
But i should get 05-02-2013 2:00 PM
why this time difference and shows wrong any idea?
The time difference is not wrong. You are getting the correct date and time for a timezone at +0:00. To fix this, set your timezone.
your formatters seems to be incorrect, to get the desired output use the code below. H is used for 24 hour format with leading zero. h is used for 12 hour format.
date("h:i A");
$eve['start']['dateTime'] = "2013-05-02T14:00:00+05:30"; // Missing quotes in your code?
$current_date = date("m-d-Y",strtotime($eve['start']['dateTime']));
Should return 05-02-2013 08:30 AM because your server has timezone sat to GMT+0. If you take 14:00 and subtracts with 5 and a half hour (from +0530 to +0000), it should be 08:30.
To avoid this, you have to set default timezone on your server or in your script.

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?

how this code work ?? and what is the right code

This is my current code that doesn't seem to work correctly.
echo date("h:i", 1*60*60) ; /* 1*60*60 mean 1 hours right */
The result is 03:00 when it should be 01:00.
Where is this going wrong?
i want to do this
1- employee come in time admin select it
2- employee come - leave saved in mysql as timestamp
Now I'm trying to make admin see how many hours user late
mean
user date time default late
user1 11-09-2011 09:10 09:00 10 min
user1 12-09-2011 08:00 09:00 -60 min
If you output just date("Y-m-d H:i:s",0) you should see that it's not 1970-01-01 00:00:00 as it should be. It's because date is affected by your local timezone. For example I'm in GMT +3, and date("Y-m-d H:i:s") gives me 1970-01-01 03:00:00.
So the answer is you are not in GMT timezone (probably in GMT+2), and date is affected by it.
UPDATE
The following code outputs 1970-01-01 00:00:00, so it's definitely time zones.
date_default_timezone_set('UTC');
echo date("Y-m-d H:i:s", 0);
Hovewer, I can't see any mention about it in PHP's date manual.
The problem is due to your timezone (looks like GMT+2).
Date calculations in PHP are based on the configured server timezone. For example mine shows
$ php -r 'echo date("h:i", 3600), PHP_EOL;'
11:00
The second argument in date() function must be UNIX timestamp, seconds passed from Jan 1 1970. You need to make time according to that value.
You have probably not setup time zones, which should produce a PHP warning or notice if omitted.
It occurs to me that what SamarLover think he wants is
gmdate("h:i", 1 * 60 * 60);
Err, if I'm right, date()'s second param is a unix timestamp, so the seconds since 1970.
You have to get time() and add 60*60 to it.
echo date("h:i", time()+60*60); // get current timestamp, add one hour

Categories