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.
Related
Extreme PHP newbie here - I am trying to create a PHP variable that will be "CURRENT DATE + 7 Days"
Something like :
date('D-m-y H:i:s', strtotime(DateTime("+7 day"))
However, I need it to output in a format like this: "30 November 2015 09:00:00"
Any ideas?
Thanks in Advance!
You can check the manual for valid date formats and change your format string.
You're basically looking for date('j F Y H:i:s', strtotime("+7 day"))
Personally, I recommend working with DateTime if you're storing this in a variable and working with it, because it becomes more convenient to extract the formatted date from the object at your conveience any time without having to go back through date and strtotime each time. Also there are numerous other benefits like not losing timezone information during conversion or having to change global timezones that effect the conversion, etc...
Example
$date = new DateTimeImmutable; // today's date
echo $date->modify('+7 days')->format('j F Y H:i:s'); // 7 days from today
echo $date->modify('-7 days')->format('j F Y H:i:s'); // 7 days ago
i am trying to get the day of week from a timestamp:
an example of the timestamp could be:
2014-09-14 18:28:11
I have tried with the following code:
$date = date("D", strtotime($activity[$i]['timestamp']));
However the result i get here is:
Thu
which should have been sunday?
Also is it possible to get it as a full discription instead of a short version of the day name?
Answer to part two of the question is that you can just use l (lowercase 'L') and it'll output Sunday instead of Sun.
$date = date("l", strtotime($activity[$i]['timestamp']));
As for the first part, it probably output Thursday, 1 January 1970 because it received an error instead of an actual date as argument to strtotime.
I am trying convert a utc time stored date to another time zone but i cant seem to get it right.
I have a time :
date1 = new DateTime('first day of the month');
date1.setTime(0,0,0); // Since using the first day of the month seems return the current time with different date
The default DateTime timezone is in UTC. The time i want to make reference is in 'Europe/Amsterdam' timezone. Any way i cant get the time in 'Europe/Amsterdam' timezone to be equivalent to the first day of the month time in UTC? (Uh, sorry my question was confusing.. let me just give an example to be clear). Im trying to query from a db.
If UTC date time is June 01, 2013. 00:00:00
I want to get get May 29, 2013 19:55:00.
I tried getting the difference between the two declared times with different timezones to get the time that i wanted but it seems it didnt work :(
My Edit/ Clarification:
If use this code:
$date1 = new DateTime('first day of the month');
$date1.setTime(0,0,0);
print_r($date1->format('Y-m-d H:i:s'));
I would get:
2013-06-01 00:00:00
Then if i use timezone:
$date1->setTimeZone(new DateTimeZone('Europe/Amsterdame'));
print_r($date1->format('Y-m-d H:i:s'));
I would get: (This is just a sample output):
2013-06-01 03:00:00
Because of time difference. Want i want to get is like the reverse: I want to get the datetime that when converted 'UTC' timezone i would get this: 06-01-2013 00:00:00 time. So my preffered output is : 2013-05-29 21:00:00 ...
You can do in an OOP way like so.
$date = new DateTime('2000-01-01 00:00:00', new DateTimeZone('Europe/Amsterdam'));
echo $date->format('Y-m-d H:i:s P') . "\n";
To set the default date in PHP, you can either set it in your ini file or in a PHP file like so:
date_default_timezone_set('Europe/Amsterdam');
Then to format the date, refer to http://www.php.net/manual/en/function.date.php for formatting.
In your case this would be:
date('j M Y' time());
Where j = day, M = month and Y = year.
I have wierd issues with time / date in PHP this year. Code have not changed at all and my dates are bugged.
Code is for example:
$date = strtotime($order['date']);
$dateNew = date('Y-m-d h:i A', $date);
print $dateNew;
Returns 1969-12-31 07:00 PM for some reasson, altough:
print $order['date'];
Returns 2013-01-12 18:25:43
I'm confused because I'm quite sure that my code is correct.
I dare you to solve this bugger!
The function strtotime() was made for transform English into date format.
The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in now, or the current time if now is not supplied.
As i don't know what is really into your $order variable i will suggest 2 solutions :
Maybe you can avoid the strtotime function and replace it by date() directly like this :
$order = ['date' => '2013-01-12 18:25:43'];
$date = date($order['date']);
It works well here: http://codepad.viper-7.com/cbNA87
Or, if it's not working consider to use mktime(), it will convert the date into seconds since the epoch.
The Unix epoch is the reference point for all time stamps. PHP calculates the times from this date in seconds.
The $date should be null and your server in the east coast of the US so it's returns the epoch :)
PHP returns the date 1969-12-31 when there is not a proper date. So if you did
$date = 0;
$dateNew = date('Y-m-d', strtotime($date));
Your result would be 1969-12-31, since that is the default Unix epoch time. http://php.net/manual/en/function.time.php
Unexpected dates of "1969-12-31 07:00 PM" means something went wrong with date() .
your strototime($order['date']) is probably returning false (failing to parse it to a unix timestamp).
Try this and ensure its returning an int (not false)
var_dump($order['date'], strtotime($order['date']));
See the error state of date: http://php.net/date
See the return values of strtotime: http://php.net/strtotime
I am using date function along with strtotime function to format the date.
Ex:
<?php
$input_date = '03-JUL-09 14:53';
$formatted_date = date("Y-m-d H:i:s", strtotime($input_date));
echo $formatted_date;
?>
Gives me a expected output: 2009-07-03 14:53:00
while if the input is changed to(I am removing the minute part)-
<?php
$input_date = '03-JUL-09 14';
$formatted_date = date("Y-m-d H:i:s", strtotime($input_date));
echo $formatted_date;
?>
The output here is 1970-01-01 05:30:00, which is not at all expected.
Whats the mistake I am doing and how it can be fixed.
Thanks
Amit
1970-01-01 05:30:00 is the time in India at the Unix epoch (1970-01-01 00:00:00 GMT/UTC). This means that strtotime returned either 0, or false, which was converted to 0 by date. This is because it could not process its input, as others have explained.
The function strtotime expects to be given a string containing a US English date format and will try to parse that format into a Unix timestamp.
You can use strings like
03 july 2009
now
09-07-03
09-07-03 14:00:00
+1 week 2 days 4 hours 2 seconds
last Monday
But not incomplete date format like "09-07-03 14". The parser don't understand it and returns nothing so when you call date("Y-m-d H:i:s", strtotime($input_date)); it returns the 0 timestamp (January 1 1970 00:00:00 UTC), cause it's the default value.
You have to give a right string that can be parsed.
This looks like it comes down to the inability of strtotime() to determine which numbers are what in your 2nd date string.
My best advice is to normalize your date-time value before passing it to strtotime(). Something simple like this might be enough
if ( false === strpos( $input_date, ':' ) )
{
$input_date .= ':00';
}
03-JUL-09 14 does not seem to be a valid date string, 03-JUL-09 14:00 works fine
from the php manual: strtotime — Parse about any English textual datetime description into a Unix timestamp
1970-01-01 is a typical baseline date (in ctime for example). I believe the output is formatting a value of 0 into a date, thus the 1970, and the unrelated 05:30:00 time of day.
I believe this is a fluke in attempting to format a date that does not include all the requested time elements. Can you pad the date data to generate a complete input date?
strtotime() fails to parse the second string when you remove the minute part. It doesn't know what the extra "14" means so it returns bool(false). Calling date(false) returns the same thing as date(0), i.e. it gives you the date of the UNIX epoch: January 1, 1970 00:00:00 GMT adjusted for your local timezone.