I am trying to add 1 minute to the current time:
echo date("Y-m-d H:m:s", strtotime("+60 seconds"));
echo '<br />';
echo date("Y-m-d H:m:s");
The output is in both cases:
2012-09-02 17:09:02
2012-09-02 17:09:02
Which is obviously wrong (in both cases - the current time is in my country 1AM).
What I am doing wrong? I have saved into the datetime column the value 2012-09-02 17:09:38 and now I try to add to the current time 60 seconds and then if the difference between stored datetime and the current datetime is less than 60s, then I want to inset another row in the database...
But the problem is, the date() function displays weird output.
("Y-m-d H:i:s");
not
("Y-m-d H:m:s");
m is month, it can't be minutes as well
date("m") will show the current month numerically. So this is why your +60 seconds does not work. Use date("Y-m-d H:i:s")
Why the current time is wrong could be several reasons. Probably something with time or timezone settings on your server.
As far as timezone goes, this might help you: date_default_timezone_set
Related
Short question but I can't get my finger on it. This piece of code:
$date = '2015-12-08T00:00:00+01:00';
echo date('D', strtotime($date));
returns Mon while
$date = '2015-12-08T00:00:00';
echo date('D', strtotime($date));
returns Tue. Why is that? The +01:00 is for the timezone, but that should not affect the day in my opinion.
First I've looked up that 08-12-2015 is in fact a Tuesday, so now we know the first one is incorrect.
PHP's date() is an Unix timestamp according to their own docs.
My belief is that adding the +1 as a timezone triggers the calculation to the +0 timezone (UTC) when asking for the day of the week and therefore returns 23:00 on monday as the current UTC time.
You can specify the timezone before executing the rest of the code: date_default_timezone_set('Europe/Amsterdam');
<?php
date_default_timezone_set('Europe/Amsterdam'); //this is an example of a +1 timezone, choose one from http://php.net/manual/en/timezones.php
$date = '2015-12-08T00:00:00+01:00';
echo date('D', strtotime($date) );
?>
strtotime will parse your date string using the supplied time zone or using the default timezone if unspecified. We can't see from the code you've posted what time zone your server is configured to, but once the date is parsed and converted to your time zone, the time may legitimately occur in the previous day, hence why you're seeing 'Mon'.
Either supply a time zone in the strtotime call via the now argument or set one globally with date_default_timezone_set.
I've set the timezone on my website pages as such:
date_default_timezone_set("America/Los Angeles");
I've declared a time variable as such:
$time = date("h:i:sa");
then adding it to the database. I then retrieve the time from the database like such, trying to reformat it:
$time2 = date("h:i A", strtotime($row['Time']));
and then printing out the time:
echo $time2;
This method, however, messes up AM and PM. If $time was added to the database at 11:59:00 at night, for example, it will print out 11:59 AM, not 11:59 PM. I think the issue is that when the time is added to the database, no clear distinction is made between AM and PM. However, I'm still unsure how to fix this. I've tried adding the time with the AM PM already in it, like such:
$time = date("h:i:sa A");
and then accessing it like such:
$time2 = $row['Time'];
but this still doesn't fix the bug. Is there a way around this while keeping my 12 hour format? Or do I have to change to 24 hour format to get this to work?
Please start storing dates/times in Y-m-d/H:i:s format in DATETIME, DATE or TIME fields. Your life, and ours, would be much easier.
Until then, use this PHP solution:
$dt = DateTime::createFromFormat('!h:i:sa', $row['Time']);
echo $dt->format('H:i:s');
demo
or mysql solution:
SELECT STR_TO_DATE('08:46:07am','%h:%i:%s%p')
I've been struggling quite a while with PHP's DateTime classes. My understanding is that a UNIX-timstamp is always in UTC, regardless of the timezone.
That's why I am quite confused with this code sample.
$date1 = new DateTime("#1351382400"); // Sun Oct 28 2012 02:00:00 GMT+2 (DST)
var_dump($date1->getTimestamp()); //prints: 1351382400
$date1->setTimezone(new DateTimeZone("Europe/Stockholm"););
var_dump($date1->getTimestamp()); //prints: 1351386000
As you can see setTimezone() changes the result of getTimestamp().
Is it expected that setTimezone() affects getTimestamp()?
The amount that you're off is 3600 seconds, or 1 hour.
I think that what you're seeing is because the date you picked is the end of Daylight Savings Time in Stockholm. If you use a different date, you don't get that effect:
$now = time();
echo " now: $now\n";
$date1 = new DateTime("#{$now}");
echo " date1 here: {$date1->getTimestamp()}\n";
$date1->setTimezone(new DateTimeZone("Europe/Stockholm"));
echo "date1 Stockholm: {$date1->getTimestamp()}\n";
Output:
now: 1352321491
date1 here: 1352321491
date1 Stockholm: 1352321491
I'm not sure if this is a bug or not, but it doesn't happen if you don't pick a date on which DST is changing.
Yes, unix timestamp is the current time as per the date object or your current machine time from Epoch.
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
I'm trying to output the current date and time using this code:
$theDate = date('y-m-d H:m:s', time());
echo $theDate;
And it works fine but the output for time does not change minutes, it simply sites at HH:07:SS, so the minute sits at 07 and the only thing that changes is the seconds and hours.
Is this because of the time function inside PHP? Does it only update minutes so often? Why would it not update the minutes as well?
How can I get an output the same but with minutes showing correctly?
Whenever i run strftime on the server it outputs fine, just trying to figure it out above.
Use i not m:
$theDate = date('y-m-d H:i:s'); echo $theDate;
07 is July :)
m represents months, not minutes. You need to use i for minutes. See the date() manual page for more information.
$theDate = date('y-m-d H:i:s'); echo $theDate;
Your format string is wrong.
It could be: y-m-d H:i:s
You dont need to give date function the second parameter time().
Just try date("format string");