473,326 Members | 2,126 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

A quick one - COnvert Date

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
2 3230
> 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.