I am having a little trouble with changing the format of a date with php, the format in question is dd/mm/yy, however I wanting to change that to yyyy-mm-dd.
I have tried doing this,
date('Y-m-d', '23/04/10')
The second argument for date() should be unixtimestamp and not a string like you have. Use strtotime() (you might need to change the string, read up on the manual!) to convert your string to unixtimestamp first.
date('Y-m-d', strtotime('23/04/10'))
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"));
Paypal returns a timestamp of the following format:
yyyy-MM-ddTHH:mm:ssZ
And I don't quite know what to do with it...
How can I convert it to yyyy-MM-dd HH:mm:ss using my local timezone in php?
I'm tempted to preg_replace the mysterious letters, but something tells me there must a better way. There also appears to be 8 hours difference to my zone which I'm not sure how to substract.
Use DateTime class to do your magic.
$date = new DateTime('2012-09-09T21:24:34Z');
$date->format('Y-m-d'); # read format from date() function
You can use strtotime() to get a UNIX timestamp. From there you can do whatever you need: DateTime object, date(), etc.
Example with date():
echo date('r', strtotime('2012-09-10T10:00:00Z'));
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
How would you convert a datestring that will be formated like...
m/d/yyyy H:i:s or mm/dd/yyyy H:i:s...
and format it like... yyyy-mm-dd H:i:s
I can format either of the two inputs into my desired format, but not both.
function format_date($date){
return date('Y-m-d H:i:s',strtotime($date));
}
should do the trick.
Easiest way would be to simply transform it to unix time first with strtotime then run strftime on it afterwards... not the bes practice but it eliminates alot of the potential format parsing issues :-)
How are you formatting it now? If you're certain that those are the only two date formats you'll possibly have as input, then you can explode() it on the forward slashes, then if strlen() returns 1 on the month or the date, add a 0 as you're creating your yyyy-mm-dd string.
However, if you're not guaranteed that those are your only two input formats, then I would use strtotime() to convert it to epoch, then use date() to format it as you desire. A slightly bigger processing hit, but much more universal code.
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.