Convert specific string to time in PHP - php

I need to convert a string into date format, but it's returning a weird error. The string is this:
21 nov 2012
I used:
$time = strtotime('d M Y', $string);
PHP returned the error:
Notice: A non well formed numeric value encountered in index.php on line 11
What am I missing here?

You're calling the function completely wrong. Just pass it
$time = strtotime('21 nov 2012')
The 2nd argument is for passing in a timestamp that the new time is relative to. It defaults to time().
Edit: That will return a unix timestamp. If you want to then format it, pass your new timestamp to the date function.

To convert a date string to a different format:
<?php echo date('d M Y', strtotime($string));?>
strtotime parses a string returns the UNIX timestamp represented. date converts a UNIX timestamp (or the current system time, if no timestamp is provided) into the specified format. So, to reformat a date string you need to pass it through strtotime and then pass the returned UNIX timestamp as the second argument for the date function. The first argument to date is a template for the format you want.
Click here for more details about date format options.

You are using the wrong function, strtotime only return the amount of seconds since epoch, it does not format the date.
Try doing:
$time = date('d M Y', strtotime($string));

For more complex string, use:
$datetime = DateTime::createFromFormat("d M Y H:i:s", $your_string_here);
$timestamp = $datetime->getTimestamp();

Related

strtotime() in php returning always 01-01-1970

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');

Convert yymmddhhmm date/time string to PHP timestamp

I have a string that is a 10 digit date/time representation in the format of yymmddhhmm, e.g:
1304282240
is the 28th of April, 2013 22:40 (28/04/13 in the DD/MM/YY format)
I need to convert this into a valid PHP timestamp but I'm getting stuck using the Date and strtotime functions, and not sure if that is the best approach in any case.
Update: I've added these lines to my code:
$date = DateTime::createFromFormat('ymdHi', '1304282240');
echo $date->format('m/d/Y h:i:s A');
appears to work
I believe you want something like this:
$date = DateTime::createFromFormat('ymdHi', '1304282240');

Date / Time showing odd value in PHP

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

PHP date format error

I have an array with a key timestamp with the following content
"timestamp" => "2011-11-29 00:00:00"
When i try to change the format using this
date("F j, Y", $data['Visitor']['timestamp']);
i get the following error
A non well formed numeric value encountered
You should be using the strtotime on the datetime data to convert it into Unix timestamp first.
date("F j, Y", strtotime($data['Visitor']['timestamp']));
Checkout the documentation of date it accept a Unix timestamp as a second parameter and you are passing a datetime value.
DEMO
The function requires the Unix Time which is numeric - and not a string formatted date.
As #Shakti Singh mentions you should use strtotime for that.
From the PHP docs on the timestamp parameter:
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().
A newer way to do this as of PHP 5.2 is the DateTime class:
$datetime = new DateTime('2011-11-29 00:00:00');
echo $datetime->format('F j, Y');
See it in action

Convert time to readable format

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

Categories