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.
Related
I have no idea about output of this function.
" strtotime(date("j"),date("F"),date("t")) "
Output: 1475734243
That output is changing according to the time.
my code block is:
<?php
$day= date("j");
$month= date("F");
$year= date("Y");
//calendar Variables
$currentTimeStamp= strtotime($day-$month-$year);
echo $currentTimeStamp;
?>
date("j") gives Day of the month without leading zeros (1 to 31).
date("F") gives A full textual representation of a month, such as January or March.
date("Y") gives A full numeric representation of a year, 4 digits (eg: 2016).
I am not sure what part is confusing but if I got it right that you want to display the current date with that format then you should use this without having to use strtotime()
echo date("j F Y");
Sample working output:
https://repl.it/Dodz/0
The output is a unix time stamp.
echo $day.'-'.$month.'-'.$year;
echo date('j-F-Y', strtotime($currentTimeStamp));
This will give you the answer you are looking for.
See strtotime documentation.
It returns the number of seconds passed since the U-day (January 1 1970 00:00:00 UTC) to the date (in string representation) that is provided as an argument. It's also called "Unix timestamp" and "Unix epoch (time)"
I am using a variable to get the date from the database,then i am passing that variable to strtotime function to get the desired format,but it always returning wrong date.perhaps there is a problem in passing a variable in strtotime function.please suggest me guys,how should i get the correct date in correct format.
Here is what i am trying to do
$date = $fetch_user['date'];
$newDate = date("d-m-Y", strtotime($date));
$day = date('l', strtotime($newDate));
echo $newDate;
echo "-----";
echo $day;
exit;
January 1, 1970 is the so called Unix epoch. It's the date where they started counting the Unix time. If you get this date as a return value, it usually means that the conversion of your date to the Unix timestamp returned a (near-) zero result. So the date conversion doesn't succeed. Most likely because it receives a wrong input.
In other words, your strtotime($date) returns 0, meaning that $date is passed in an unsupported format for the strtotime function.
So you'll have to check for yourself $date, before calling strtotime at all.
01-01-1970 means you probably get 0 as a result of strtotime(). You are probably using a format, that this function cannot understand. The PHP documentation states:
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.
So it is not really flexible. You might want to try DateTime::createFromFormat instead. Take a look at it's documentation.
Basically you have to specify the format of the date string, that you give as an input as well. That way you can use whatever date format you want.
Example from php.net:
<?php
$date = DateTime::createFromFormat('j-M-Y', '15-Feb-2009');
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 have been looking online for this answer and have come up empty...I am extremely tired so I thought I would give this a go....
I have a variable that has a date from a textbox
$effectiveDate=$_REQUEST['effectiveDate'];
What I am trying to do is take this date and add the current time
date('Y-m-d H:i:s', strtotime($effectiveDate))
When I echo this out I get 1969-12-31 19:00:00
Is this possible? Can someone point me in the right direction?
I found a solution to my problem....
$currentDate = date("Y-m-d");
$currentTime = date("H:i:s");
$currentDate = date("Y-m-d H:i:s", strtotime($currentDate . $currentTime));
echo $currentDate;
This takes a date from variable in one format and takes the date from another variable in another format and puts them together :)
Thanks everyone for their time.....
DateTime::createFromFormat
would also work but only if you have PHP 5.3 or higher...(I think)
The effectiveDate string is not in a format that strtotime recognizes, so strtotime returns false which is interpreted as 0 which causes the date to be displayed as January 1, 1970 at 00:00:00, minus your time zone offset.
The result you see is caused by the entered date not being in a format recognised by strtotime. The most likely case I can think of without knowing the format you used is that you used the US order of putting the month and day the wrong way around - this confuses strtotime, because if it accepts both then it can't distinguish February 3rd and March 2nd, so it has to reject US-formatted dates.
The most reliable format for strtotime is YYYY-MM-DD HH:ii:ss, as it is unambigous.
The date is just a timestamp, it is not object-oriented and i don't like it.
You can use the DateTime object.
The object-oriented best way is:
$effectiveDate=$_REQUEST['effectiveDate'];
// here you must pass the original format to pass your original string to a DateTimeObject
$dateTimeObject = DateTime::createFromFormat('Y-m-d H:i:s', $effectiveDate);
// here you must pass the desired format
echo $dateTimeObject->format('Y-m-d H:i:s');
I have time like this in the database
[open_time] => 10:00:00
[close_time] => 23:00:00
I want to convert it into readable form like 10:00am 11:00pm
I tried this:
$open = date("g:s a",$time['open_time']);
$close = date("g:sa",$time['close_time']);
I'm getting the following error:
A non well formed numeric value encountered
date expects an integer argument, the traditional Unix timestamp.
Try this:
date('g:s a', strtotime($time['open_time']));
strtotime attempts to convert a string into an integer Unix timestamp as expected by date.
If it's in your database, consider using the MySQL DATE_FORMAT() function directly in your request
The second argument needs a UNIX timestamp (epoch time):
http://php.net/manual/en/function.date.php
timestamp
The optional timestamp
parameter is an integer Unix timestamp
that defaults to the current local
time if a timestamp is not given. In
other words, it defaults to the value
of time().
<?php
$t = time();
print_r($t);
print_r(date('g:i a', $t));
?>
gives
1288935001
10:30 pm