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
Related
Code:
$time = strtotime('2020-03-31');
echo date('Y-m-d', strtotime('-1 month', $time));
Expected Result: Any date from Feb 2020
Actual Result: 2020-03-02
Is there any better way to add or subtract a month from a given date?
Months are an awkward interval to work with, because they don't have a fixed length. Should the algorithm assume that by "1 month" you mean "30 days", or "31 days", or should it just try subtracting 1 from the "month" field in the date structure?
The last option is what is happening here: given "2020-03-31", PHP's date library is subtracting 1 from the "03" to give "2020-02-31". Since that's an invalid date (February 2020 had 29 days), it then "normalises" it to a real date - 2 days after the 29th February was the 2nd March.
Probably you want to use a more specific period to subtract, like 30 days - although note that if the initial input is "2020-03-01" that will give you "2020-01-31", not "2020-02-01".
Ultimately, this is a problem with our irregular calendar, rather than with PHP. It's really up to you to define what you mean by "a month before", and use a more specific algorithm that captures that requirement.
You can make code like below
<?php
$time = strtotime('2020-03-1 -32 days');
echo date('M-Y', $time); // output Feb-2020
?>
The above code will return date as you expected
I have the following code:
$date = date_create('2014-11-06T23:00:00.000Z');
date_timezone_set($date, timezone_open('Australia/Victoria'));
echo $date ->format('Y-m-d H:i:s');
And it results in:
2014-11-07 10:00:00
However in the PHP doco here it says that capital H is 24hr time:
http://php.net/manual/en/function.date.php
H 24-hour format of an hour with leading zeros 00 through 23
If I comment out the middle line as follows:
$date = date_create('2014-11-06T23:00:00.000Z');
#date_timezone_set($date, timezone_open('Australia/Victoria'));
echo $date ->format('Y-m-d H:i:s');
It results in:
2014-11-06 23:00:00
Which does show 24hr time, but the date is a day out, which is why I need to set the timezone.
Why does setting the date_timezone_set stop me from being able to show the date in 24hr time format, and how to do fix this?
Unless you specify a timezone date_create assumes the current timezone. If you don't define one then it will use UTC.
Since the timezone is specified after the date_create the date is stored as UTC.
Since 2014-11-06 23:00 UTC is the same time as 2014-11-07 10:00 UTC+11 (Australia/Victoria) you will see the latter when you 'echo' the date.
I am storing the time of day as a number of seconds since midnight. I have a number that should be 8:00 am:
//3600 secs / hour * 8 = 28800
$time = 28800;
var_dump(date('h:i a', $time ));
My output is:
string(8) "01:00 am"
Based on my location, I am -7:00 GMT, so I can see where I would get 1:00 am, but how do I do format this time to show 8:00 am, essentially making it ignore the current GMT setting while formatting this time?
two ways.
first you may try gmdate() function which output the raw GMT time .
and the other way you can set timezone before you use date function.
as follow .
date_default_timezone_set('Asia/Shanghai');
echo date('H:i:m', time());
I figured this out. The solution is to use gmdate(). It will format a raw timestamp to GMT.
I am writing a function to convert an internal date format into a timestamp value. However, when I print the date out in YYYY-MM-DD HH format the date is 12 hours off.
The code below gives me the wrong date and time. I am expecting 2011-03-25 13 but instead I am getting 2011-03-25 01.
date_default_timezone_set("Europe/London");
$epoch = mktime(13,0,0,3,25,2011);
echo date('Y-m-d h', $epoch);
When I use the following code I expect 2001-02-01 01 and get what I expected.
date_default_timezone_set("Europe/London");
$epoch = mktime(1,0,0,2,1,2011);
echo date('Y-m-d h', $epoch);
It seems that the 12 hour offset starts on March 25th in the 13th hour.
Any idea why this happens and how do I keep it from happening? Does this have to do with the different day light savings dates? The server timezone is set to "America/Los_Angeles".
It works, you're just using the wrong format code, take H (24 hour format) instead of h (12 hour format):
date_default_timezone_set("Europe/London");
$epoch = mktime(13,0,0,3,25,2011);
echo date('Y-m-d H', $epoch);
Read the PHP Manual, it explains each code in detail.
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