By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,405 Members | 1,531 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,405 IT Pros & Developers. It's quick & easy.

A quick one - COnvert Date

P: n/a
Ok I have the folowing :

$theValue which is equal to $_POST['start_date']
the date is in the Format of : dd-mm-yyyy
and i want to put it in the database into the form of yyyymmdd
I do that :
$theValue = date('Ymd',strtotime($theValue));

but it is WRONG it returns -1 or 19700101

what should I do ?
to convert dd-mm-yyyy into yyyymmdd in order to store it in a database as
integer and use it later to make calculations between two dates ?

Thanks
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
> to convert dd-mm-yyyy into yyyymmdd in order to store it in a database as
integer and use it later to make calculations between two dates ?


Propably that Answers my question:

function formatStringAsDate( $date ) {
$day = "00";
$month = "00";
$year = "0000";

if ( $date != "" ) {
// yyyy-mm-dd
if ( ereg( "([0-9]{4})-([0-9]{2})-([0-9]{2})", $date, $components )) {
$day = $components[3];
$month = $components[2];
$year = $components[1];

// dd-mm-yyyy
} else if ( ereg( "([0-9]{2})/([0-9]{2})/([0-9]{4})", $date, $components ))
{
$day = $components[1];
$month = $components[2];
$year = $components[3];
}

$day = ( strlen( $day ) == 1 ) ? "0".$day : $day;
$month = ( strlen( $month ) == 1 ) ? "0".$month : $month;
$year = ( strlen( $year ) < 4 ) ? (( $year < 50 ) ? ( 2000 + $year ) : (
1900 + $year )) : $year;

return $day."/".$month."/".$year;
}
Jul 17 '05 #2

P: n/a
Angelos wrote:
Ok I have the folowing :

$theValue which is equal to $_POST['start_date']
the date is in the Format of : dd-mm-yyyy
and i want to put it in the database into the form of yyyymmdd
I do that :
$theValue = date('Ymd',strtotime($theValue));

but it is WRONG it returns -1 or 19700101

what should I do ?
to convert dd-mm-yyyy into yyyymmdd in order to store it in a database as
integer and use it later to make calculations between two dates ?

Thanks

Angelos,

That's because the format for hyphenated date is yy-mm-dd. Probably the
easiest way is to split the date into its components and put it back
together again (warning - no validation in the following - use at your
own risk!):

list($d, $m, $y) = preg_split('/-/', $theValue);
$theValue2 = sprintf('%4d%02d%02d', $y, $m, $d);

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.