I've got an arry with dates.
print_r ($date[$i]);
will output sth. like:
2011-06-16
Is it possible to create an output like: 16.6.2011 ?
How would I do that?
have you got a reference?
<?
echo date('d.m.Y', strtotime( $date[$i] ) );
?>
you should find that this page has all the answers for you
http://php.net/manual/en/function.date.php
You can use the date function to print it, using the strtotime function to first convert it to a Unix timestamp. Or you can use the DateTime functions like:
date_create($date[$i])->format('d.n.Y');
If you don't want leading 0's on your day, then you should use:
date_create($date[$i])->format('j.n.Y');
use the php date() function
you can see a documentation about it here
http://php.net/manual/en/function.date.php
You don't really have a date, but a string. Either manipulate the string with the string manipulation functions, or parse and re-format the date string with the date functions.
date('d.n.Y', strtotime($date[$i]));
There is a reference when you search in Google for "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"));
I need help to convert a formatted date format as stated below:
How can I convert 2010-02-06 14:44:43 to dd/mm/yyyy
Thanks
Great place to start for this type of general information, php docs, specifically related to your date format question: http://us3.php.net/manual/en/function.date.php
-- Edit --
Answer with substr() forced me to do the work... using substr() to format a date is not the best option as PHP has built-in functions for that.
var_dump(date('d/m/Y', strtotime('2010-02-06 14:44:43')));
$date = new DateTime('2010-02-06 14:44:43');
echo $date->format('d/m/Y');
Output:-
06/02/2010
See the manual
Try this:
<?php
$time = strtotime('2010-02-06 14:44:43');
echo date('d/m/Y',$time);
?>
All,
I have the following string:
$dateTime = '2013-09-15T00:00:00.000Z';
Is there a function to extract Year, Month and Date from the above string, so the result looks like the following:
$yearMonthDate = '2013-09-15';
Thanks
You could convert your datetime to a timestamp using strtotime() and then convert it back into a formatted date using this kind of syntax:
date("Y-m-d", strtotime($myOriginalDate))
substr or DateTime or strtotime+date
Since the first string is actually a standard, you can just use substr:
$yearMonthDate = substr($dateTime, 0, 10);
However, that would be kind of a hack and would obviously break if the format of $dateTime were to change. So, you might want to look into the PHP DateTime class instead.
Hi pretty much what it says on the tin.
I have a datetime mysql field I want to output in the format dd/mm/yyyy hh:mm like 07/01/2011 22:16.
I've tried:
<?php
$datestring = '%d/%m/%Y %h:%i';
echo mdate($datestring,$row->created);
?>
But I'm getting an error:
Message: A non well formed numeric value encountered
Any help most appreciated!
Cheers,
Billy
Try:
echo date ("d/m/Y h:ia",strtotime($row->created));
The second parameter of the mdate() function still needs to be an integer timestamp, just like the native PHP date() function. Try using the strtodate() function which accepts a string as a parameter (including the MySQL date format) and returns an integer. This can be done like this:
$datestring = '%d/%m/%Y %h:%i';
echo mdate($datestring, strtodate($row->created));
The only difference between mdate() and date() is, as the CodeIgniter docs say:
This function is identical to PHPs date() function, except that it lets you use MySQL style date codes, where each code letter is preceded with a percent sign: %Y %m %d etc.
The benefit of doing dates this way is that you don't have to worry about escaping any characters that are not date codes, as you would normally have to do with the date() function.
Got this to work using treeface's solution, with one minor change:
$datestring = '%d/%m/%Y %h:%i';
echo mdate($datestring, strtoDATE($row->created));
//strtoDATE didn't work but strtoTIME did
Had me scratching my head for hours, but now it works, I'm able to keep using CI helper for all date functions.
HTH
I'm using:
mdate(date_string,mysql_to_unix($row->created))
That should work.
Is there a way to change a date from
1985-12-15
to
1985-12
without using a regular Expression?
<?php echo date('Y-m', strtotime('1985-12-15')); ?>
That should do it.
This will, using strtotime, convert 1985-12-15 to a unix timestamp. The date function then takes a second parameter timestamp on which to format the date.
Convert the date to time via strtotime then use date to output in correct date format, like so:
<?php
echo date('Y-m', strtotime('1985-12-15'));
Maybe I'm just stupid, but if you only want the beginning of that date, stored as a string, can't you just use substr to extract the 7 characters at the beginning of that string ?
A bit like this, for instance :
$input = '1985-12-15';
$output = substr($input, 0, 7);
var_dump($output);
Which does give you :
string '1985-12' (length=7)
No need for any date-manipulation related function, in this case -- and this will probably be even faster/cheapier that parsing the string to a date and all that.
(Yeah, I know, premature optimisation ^^ )
$myDate = date('Y-m',strtotime('1985-12-15'));
echo $myDate // prints '1985-12'
If you think a regular expression isn't "cheap", then time functions will almost certainly be even more expensive: you would need to convert the string into a time value, then format it back into a string...
do you have that time value already in a array or do you whant such a like output come from the date function?
If you are not sure how the date will be formated, you should use the strtotime function, otherwise its probably as easy to do if the format is yyyy-mmm-dd or yy-m-d.
$datearray = explode('-',$date);
echo $datearray[0].'-'.$datearray[1];
not best to substr($date,0,strrpos($date,'-'));?
that would be cheapest?