I meet a trouble with php data function.
echo strtotime("31/05/2011");
It prints empty.
What's problem with this?
Thanks.
DD/MM/YYYY Is not a valid date format (The manual outlines it must follow one of the supported date and time formats.) So, instead, it should be MM/DD/YYYY:
echo strtotime("05/31/2011");
Or, I guess as others have posted, the european (ISO8601 Notations) version uses hyphens:
echo strtotime("31-05-2011");
http://php.net/manual/en/function.strtotime.php
Use dashes instead of forward slashes.
echo strtotime("31-05-2011"); // outputs 1306821600
For European formatted dates (DD-MM-YYYY) use dashes not slashes:
echo strtotime('31-05-2011');
echo strtotime("2011-05-31");
How about using php's DateTime functions?
DateTime::createFromFormat('d/m/Y', '31/05/2011');
Related
I have a date field that I would like to express through a MySQL Query or as a PHP function as yyyymmdd, basically without any - or /, all the examples I can find include these.
I could build it a piece at a time, so y.m.d, but thought someone here might have come across this for, its basically a naming convention for network folders, hence why no special characters.
Thank you in advance
You can use PHP's DateTime Class:
echo (new DateTime($dateField))->format('Ymd');
SQL you can use DATE_FORMAT
DATE_FORMAT(NOW(),'%Y%m%d') //20150513
PHP you can use using date
echo date('Ymd'); //20150513
You can use strtotime() and date() function like this:
$time = strtotime($yourdatefield);
$date = date('Ymd', $time);
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);
?>
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" ;)
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?