Just a simple question. How do I convert a PHP ISO time (like 2010-06-23T20:47:48-04:00) to something more readable? Is there a function already built in PHP? I've looked around but I haven't seen anything to convert times. If there's not a function, is it possible?
Thank you
$format = "d M Y"; //or something else that date() accepts as a format
date_format(date_create($time), $format);
Try this:
echo date( "Y-m-d H:i:s", strtotime("2010-06-23T20:47:48-04:00") );
Format this part "Y-m-d H:i:s" using format from this documentation http://php.net/manual/en/function.date.php
echo strftime("%b %e %Y at %l:%M %p", strtotime($ios));
will do
strptime() converts a string containing a time/date with the format passed as second argument to the function. The return value is an array containing values for day, month, year, hour, minutes, and seconds; you can use those values to obtain a string representing the date in the format you like.
strptime() is available since PHP 5.1.0, while the class DateTime is available since PHP 5.2.0.
I think you should try strftime
http://php.net/function.strftime
Sounds like you're looking for the date function: http://php.net/function.date
Possibly paired with the strtotime function: http://php.net/function.strtotime
For a short date try this:
$a_date = '2010-06-23T20:47:48-04:00';
echo date('Y-m-d', strtotime($a_date));
Related
I have a php string from db it is 20/11/2017 I want to convert it milliseconds.
It's my code to doing that.
$the_date = "20/11/2017";
$mill_sec_date = strtotime($the_date);
var_dump($mill_sec_date);
But it does not print any thing rather than
bool(false);
What is the problem and how can i solve it ????
When using slashes to separate parts of the date, PHP recognizes the format as MM/DD/YYYY. Which makes your date invalid because there is no 20th month. If you want to use the format where day and month is swapped, you need to use hyphens, like DD-MM-YYYY.
$time = strtotime('10/16/2003');
$newformat = date('Y-m-d',$time);
print_r($newformat);
Use DateTime class to call function createFromFormat
$date = date_create_from_format('d/M/Y:H:i:s', $string);
$date->getTimestamp();
Most likely you got the date format wrong, see
here for a list of supported date and time formats:
This section describes all the different formats that the strtotime(), DateTime and date_create() parser understands.
You string is not accept by the strtotime, you can use createFromFormat set set the with the format type of the time string like below, you can also check the live demo. And you also can refer to this answer
var_dump(DateTime::createFromFormat('d/m/Y', "20/11/2017"));
How to convert this (in ISO8601 format): 2014-03-13T09:05:50.240Z
To this (in MySQL DATE format): 2014-03-13
in php?
try this
$date = '2014-03-13T09:05:50.240Z';
$fixed = date('Y-m-d', strtotime($date));
The complete date function documentation can be found here: http://php.net/manual/en/function.date.php
The PHP function "strtotime" does nothing else then converting your timestring into an unix timestamp.
Hope I could help :)
P.s.:
Just in case strtotime will return 0 try using this:
$date = '2014-03-13T09:05:50.240Z';
$fixed = date('Y-m-d', strtotime(substr($date,0,10)));
Since PHP 5.2.0 you can do it using OOP and DateTime() as well (of course if you prefer OOP):
$now = new DateTime("2014-03-13T09:05:50.240Z");
echo $now->format('Y-m-d'); // MySQL datetime format
There is no reason to use the inefficient time functions. The most efficient way is to simply extract the first 10 characters:
substr($date,0,10)
People, that are really coding for year ≥10000, can use:
substr($date,0,strpos($date,"T"))
Simply convert datetime description into a Unix timestamp using with strtotime and then five format using Date Formats
Try it will surely work for you.
$date = '2014-03-13T09:05:50.240Z';
$fixed = date('Y-m-d', strtotime($date));
For those using Carbon (php library), the parse() works quite well:
Carbon::parse($date)
https://carbon.nesbot.com/docs/
Today I have published an interitty/utils package that deals with, among other things, the ISO-8601 format and perhaps all permutations of this standard.
I hope it will help you too.
$dateTimeFactory = new Interitty\Utils\DateTimeFactory();
$dateTime = $dateTimeFactory->createFromIso8601('1989-12-17T12:00:00Z');
strtotime() in PHP works great if you can provide it with a date format it understands and can convert, but for example you give it a UK date it fails to give the correct unix timestamp.
Is there any PHP function, official or unofficial, that can accept a format variable that tells the function in which format the date and time is being passed?
The closest I have come to doing this is a mixture of date_parse_from_format() and mktime()
// Example usage of the function I'm after
//Like the date() function but in reverse
$timestamp = strtotimeformat("03/05/2011 16:33:00", "d/m/Y H:i:s");
If you have PHP 5.3:
$date = DateTime::createFromFormat('d/m/Y H:i:s', '03/05/2011 16:33:00');
echo $date->getTimestamp();
You are looking for strptime, I think. you can use it to parse the date and then use mktime if you need a UNIX timestamp.
function strotimeformat($date, $format) {
$d = strptime($date, $format);
return mktime($d['tm_hour'], $d['tm_min'], $d['tm_sec'],
$d['tm_mon'], $d['tm_mday'], $d['tm_year']);
}
This will work with PHP 5.1 and onwards.
strtotime assumes it's a US date/time when using / as the separator. To get it to think it's a Euro date/time, use - or . as the date separator. You can change the /s to -s or .s with a simple str_replace()
I want to convert date 24/09/2010 in format dd/mm/yyyy to 2010-09-24 in format yyyy-mm-dd.
This works:
date("Y-m-d",strtotime("09/24/2010"));
But this does not:
date("Y-m-d",strtotime("24/09/2010")); // it returns '1970-01-01'
Any idea why?
according to php, the valid php formats are stated here. So basically what you gave is invalid.
Alternatively, you can use mktime, date_parse_from_format or date_create_from_format
strtotime does its best to guess what you mean when given a string, but it can't handle all date formats. In you example, it is probably thinking that you are trying to refer to the 24th month, which isn't valid, and returns 0, which date then treats as the unix epoch (the date you got).
you can get around this using the mktime() and explode() functions, like so:
$date = "24/09/2010";
$dateArr = explode("/",$date);
$timeStamp = mktime(0,0,0,$dateArr[1],$dateArr[0],$dateArr[2]);
$newFormat = date("Y-m-d",$timeStamp);
As you say,
date("Y-m-d",strtotime("09/24/2010"))
will work,because the date format--"09/24/2010"is correct,
but "24/09/2010" is not the correct date format.
you can find something useful here
I have a string as mentioned below:
$ts = "3/11/09 11:18:59 AM";
which I got using the date() function.
Now I need to convert this to a readable format like below
11-Mar-2009
I have tried everything using date(). How can I achieve this?
You need to convert it to something you can use for further formatting. strtotime() is a good start, which yields a unix timestamp. You can format that one using strftime() then.
strftime("%d-%b-%G", strtotime($ts));
Actually I tried doing this and it worked.
echo date("d-M-Y", strtotime($ts));
If you initially get the string from the date() function, then pass on formatting arguments to the date-function instead:
date('Y-m-d')
instead of converting the string once again.
EDIT: If you need to keep track of the actual timestamp, then store it as a timestamp:
// Store the timestamp in a variable. This is just an integer, unix timestamp (seconds since epoch)
$time = time();
// output ISO8601 (maybe insert to database? whatever)
echo date('Y-m-d H:i', $time);
// output your readable format
echo date('j-M-Y', $time);
Using strtotime() is convinient but unessecary parsing and storage of a timerepresentation is a stupid idea.
You can use the date() function to generate the required format directly, like so:
date("j-M-Y");
See www.php.net/date for all the possible formats of the output of the date() function.