2017-05-28 14 views
-2

日時変換PHP

<?php 
 
//Connection Variable to establish connection to the database parameters and localhost, user, password 
 
$connection = mysql_connect("localhost","root","") or die(mysql_error()); 
 

 
//selects the database called trial and uses the connection details 
 
mysql_select_db("bb",$connection); 
 

 
//Checks to see if a file has been submitted 
 
if(isset($_POST['submit'])){ 
 

 
\t // if submitted 
 

 
/*Propriety Information if user needs to be added*/ 
 
/* 
 

 
$gym=mysql_real_escape_string($_POST['Gym']); 
 
$access=mysql_real_escape_string($_POST['accessID']); 
 
$Chal=mysql_real_escape_string($_POST['Chal']);  
 

 

 
*/ 
 

 
//Files path with the added extention of a random name at the end. 
 
$file=$_FILES['file']['tmp_name']; 
 
echo $file; 
 
//Force Open the file in read state 
 
$handle = fopen($file, "r"); 
 

 
fgetcsv($handle,1000,","); 
 
fgetcsv($handle,1000,","); 
 
fgetcsv($handle,1000,","); 
 

 
//initates a check for the csv 
 
if(($filedata =fgetcsv($handle,1000,",")) !== false) 
 
{ 
 

 
\t //While loop with the ammount of data in the csv, fgetcsv grabs all the data that is open. Each cell is restricted to 1000 charachters, then seperated by a comma 
 

 
\t while(($filedata =fgetcsv($handle,1000,",")) !== false) 
 
\t { 
 
\t \t \t //Cycles through the row and each cell is then put into an array 
 

 
\t \t \t // Multidimensional array tutorial E- Excel, DBS - Database 
 
\t \t \t \t \t \t \t \t \t \t 
 
\t \t \t $userIDP2 = $filedata[0];  // E 1. User ID , DBS - userid 
 
\t \t \t $height = $filedata[1]; // E 2. Height, DBS - height 
 
\t \t \t $genderIdentifier = $filedata[2]; // E 3. gender, DBS - gender (UserTable) 
 
\t \t \t $testDateChange = $filedata[4]; // E 5. Test Date/Time, DBS - testDate 
 
\t \t \t $weight = $filedata[5]; // E 6. Weight, DBS - weight 
 
\t \t \t $bodyFatMass = $filedata[23]; // E 24. BFM (Body Fat Mass), DBS - bodyFatMass 
 
\t \t \t $SkeletalMuscle = $filedata[32]; // E 33. SMM (Skeletal Muscle Mass), DBS - SkeletalMuscle 
 
\t \t \t $bodyFatP = $filedata[38]; // E 39. BFP (Body Fat Percentage), DBS - bodyFatP 
 
\t \t \t $lean_RA = $filedata[62]; // E 63. LM of Right Arm,    DBS - lean_RA 
 
\t \t \t $lean_LA = $filedata[64]; // E 65. LM of Left Arm,    DBS - lean_LA 
 
\t \t \t $lean_Trun = $filedata[66]; // E 67. LM of Trunk (Abdomin), DBS - Lean_Trun  
 
\t \t \t $lean_RL = $filedata[68]; // E 69. LM of Right Leg,    DBS - lean_RL 
 
\t \t \t $lean_LL = $filedata[70]; // E 71. LM of Left Leg,    DBS - lean_LL 
 
\t \t \t $wc_TargetW = $filedata[73]; // E 74 Target Weight,    DBS - wc_TargetW 
 
\t \t \t $BMR = $filedata[77]; // E 78.BMR,        DBS - BMR 
 
\t \t \t $visceralFat1 = $filedata[81]; // E 82.Visceral Fat Level,  DBS - visceralFat 
 
\t \t \t 
 
\t \t \t /*Replaces specific characters strings for the associated data */ 
 
\t \t \t $userIDR2 = str_replace("<","",$userIDP2); 
 
\t \t \t $userID = str_replace(">","",$userIDR2); 
 
\t \t \t $visceralFat = str_replace("level","",$visceralFat1); 
 

 
\t \t \t $HyphenDate = str_replace('.','-' , $testDateChange); 
 

 
\t \t \t $date = DateTime::createFromFormat('d-m-Y H:i:s', $HyphenDate); 
 
\t \t \t echo $date->format('Y-m-d h:i:s'); 
 
\t \t \t echo $date; 
 

 
\t \t \t //Variable Declare 
 
\t \t \t $Gender =''; 
 

 
\t \t \t // To check if Male or Female 
 
\t \t \t if($genderIdentifier == 'M' || $genderIdentifier == 'm'){ 
 
\t \t \t \t $Gender = "Male"; 
 
\t \t \t }else if($genderIdentifier == 'F' || $gender == 'f'){ 
 
\t \t \t \t $gender = "Female"; 
 
\t \t \t }else{ 
 

 
\t \t \t \t $gender = "intermittent"; 
 
\t \t \t } 
 

 
\t \t \t //Change the Date Time format 
 
\t \t \t echo"<br/>"; 
 
\t \t \t 
 
\t \t \t 
 

 
\t \t 
 

 

 
\t \t \t //sql query 
 
\t \t \t $sqlBodyCompositionStart=" 
 
\t \t \t INSERT INTO `bodycomp` 
 
\t \t \t (
 
\t \t \t \t `userid`, 
 
\t \t \t \t `height` , 
 
\t \t \t \t `testDate` , 
 
\t \t \t \t `weight`, 
 
\t \t \t \t `bodyFatMass`, 
 
\t \t \t \t `SkeletalMuscle`, 
 
\t \t \t \t `bodyfatP`, 
 
\t \t \t \t `lean_RA`, 
 
\t \t \t \t `lean_LA`, 
 
\t \t \t \t `lean_Trun`, 
 
\t \t \t \t `lean_RL`, 
 
\t \t \t \t `lean_LL`, 
 
\t \t \t \t `wc_TargetW`, 
 
\t \t \t \t `BMR`, 
 
\t \t \t \t `visceralFat` 
 
\t \t \t)VALUES (
 
\t \t \t \t '".$userID."', 
 
\t \t \t \t '".$height."', 
 
\t \t \t \t '".$testDate."', 
 
\t \t \t \t '".$weight."', 
 
\t \t \t \t '".$bodyFatMass."', 
 
\t \t \t \t '".$SkeletalMuscle."', 
 
\t \t \t \t '".$bodyFatP."', 
 
\t \t \t \t '".$lean_RA."', 
 
\t \t \t \t '".$lean_LA."', 
 
\t \t \t \t '".$lean_Trun."', 
 
\t \t \t \t '".$lean_RL."', 
 
\t \t \t \t '".$lean_LL."', 
 
\t \t \t \t '".$wc_TargetW."', 
 
\t \t \t \t '".$BMR."', 
 
\t \t \t \t '".$visceralFat."' 
 
\t \t \t);"; 
 

 

 
\t \t \t $sqlBodyCompositionEnd=" 
 
\t \t \t INSERT INTO `bodycomp` 
 
\t \t \t (
 
\t \t \t \t `height_1` , 
 
\t \t \t \t `testDate_1` , 
 
\t \t \t \t `weight_1`, 
 
\t \t \t \t `bodyFatMass_1`, 
 
\t \t \t \t `SkeletalMuscle_1`, 
 
\t \t \t \t `bodyfatP_1`, 
 
\t \t \t \t `lean_RA_1`, 
 
\t \t \t \t `lean_LA_1`, 
 
\t \t \t \t `lean_Trun_1`, 
 
\t \t \t \t `lean_RL_1`, 
 
\t \t \t \t `lean_LL_1`, 
 
\t \t \t \t `wc_TargetW_1`, 
 
\t \t \t \t `BMR_1`, 
 
\t \t \t \t `visceralFat_1` 
 
\t \t \t)VALUES (
 
\t \t \t \t '".$height."', 
 
\t \t \t \t '".$testDate."', 
 
\t \t \t \t '".$weight."', 
 
\t \t \t \t '".$bodyFatMass."', 
 
\t \t \t \t '".$SkeletalMuscle."', 
 
\t \t \t \t '".$bodyFatP."', 
 
\t \t \t \t '".$lean_RA."', 
 
\t \t \t \t '".$lean_LA."', 
 
\t \t \t \t '".$lean_Trun."', 
 
\t \t \t \t '".$lean_RL."', 
 
\t \t \t \t '".$lean_LL."', 
 
\t \t \t \t '".$wc_TargetW."', 
 
\t \t \t \t '".$BMR."', 
 
\t \t \t \t '".$visceralFat."' 
 
\t \t \t);"; 
 
\t \t \t 
 
\t \t 
 
\t   \t // Check for user name 
 
\t \t \t $userNameSQL ="SELECT * 
 
\t \t \t FROM user 
 
\t \t \t WHERE username ='".$userID."'"; 
 
\t \t \t $userCheck = mysql_query($usernameSQL) or die(mysql_error()); 
 
\t \t \t \t \t 
 
\t \t \t if(mysql_num_rows($userCheck)==1) //If rows are return 
 
\t \t \t { 
 
\t \t \t \t $sqlBodyCompositionEnd; 
 
\t \t \t \t $uploadData = mysql_query($sqlBodyCompositionEnd) or die(mysql_error()); 
 
\t \t \t \t if($uploadData) 
 
\t \t \t \t { 
 
\t \t \t \t \t echo"Data has uploaded"; 
 
\t \t \t \t } 
 
\t \t \t \t else 
 
\t \t \t \t { 
 
\t \t \t \t \t echo "Data has not been uploaded"; 
 
\t \t \t \t } 
 
\t \t \t } 
 
\t \t \t else 
 
\t \t \t { 
 
    \t \t \t \t // Adds a new User 
 
\t \t \t  \t // SQL Query 
 
\t \t \t  $sql =" INSERT INTO `user`(`UserName`,`gender`,) 
 
\t \t \t \t   VALUES 
 
\t \t \t \t   ('".$userID."','".$gender."')"; 
 
\t \t \t \t   $userID = mysql_insert_id(); 
 
\t \t \t \t   $resultNA = mysql_query($sql) or die(mysql_error()); //run your SQL query 
 
\t \t \t \t \t \t   
 

 
\t \t \t \t if($resultNA) 
 
\t \t \t \t { 
 
\t \t \t \t \t echo"A new User has been added"; 
 
\t \t \t \t \t // Once a new user has been added, it then tried to add data 
 
\t \t \t \t \t $sqlBodyComposition; 
 
\t \t \t \t \t $uploadData = mysql_query($sqlBodyCompositionStart) or die(mysql_error()); 
 
\t \t \t \t \t if($uploadData) 
 
\t \t \t \t \t { 
 
\t \t \t \t \t \t echo"Data has uploaded"; 
 
\t \t \t \t \t } 
 
\t \t \t \t \t else 
 
\t \t \t \t \t { 
 
\t \t \t \t \t \t echo "Data has not been uploaded"; 
 
\t \t \t \t \t } 
 
\t \t \t \t } 
 
\t \t \t \t else 
 
\t \t \t \t { 
 
\t \t \t \t \t echo "There is no user"; 
 
\t \t \t \t } 
 
\t \t \t } 
 
\t \t } 
 
\t } 
 
} 
 

 
/* 
 

 

 
OBJECTIVE FOR FILE: 
 

 
This file grabs the file that was posted and cycles the data in the csv; to avoid duplication of pages, the bottom half the code should check to see if an existing 
 
user is there; if it is, it uses the same code that would be used if the listed memeber wasn't in the database. This means that $sqlBodyComposition should be a 
 
universal variable which will limit duplication. 
 

 
We need to create a function that checks for a user, returns true and inserts the 
 
$sqlBodyComposition query. If a user doesn't exist, we need to remove "<" and ">" 
 
in the ID insert a new user. When the user is inserted, it is to then insert that 
 
data about the body composition to the user. 
 

 
It is to return to a page and determine whether it succeeded or not. 
 

 
*/ 
 

 
?>

は、だから私は、CSVファイルの日付を持っていると私がymd hで、より適切な形式に変換するためにPHPを必要とする:I:S

形式を私は変換する必要があります 10.05.2017 18:31:07 これは変数/文字列として格納されます

私はstr_replace( '。'、 ' - '、$ date);を使用しました。

と私は今

はこれでいくつかの助けをつかむことが可能である提出可能な形式を達成するための年と日付を逆にする必要がありますか?

+0

'DateTime :: createFromFormat()'を見てください。これはあなたに 'DateTime'オブジェクトを与え、あなたは' - > format() 'を目的のフォーマットにすることができます。 – rickdenhaan

答えて

-1

文字列を並べ替えるのではなく、日付オブジェクトを作成する方が簡単かもしれません。

このコードは、あなたがやりたいことのために働く必要があります。

<?php 
    //Raw Input as string 
    $raw = "10.05.2017 18:31:07"; 
    //Create date object 
    $date = DateTime::createFromFormat('d.m.Y H:i:s', $raw); 
    //Print out date object 
    echo $date->format('Y-m-d h:i:s'); 
?> 

createFromFormatのドキュメントがここで見つけることができます:http://php.net/manual/en/datetime.createfromformat.php

コナー。

+0

ええ、私はその正確な方法を試したので、これは奇妙で、オブジェクトを再配置できませんでした。 – user3701575

+0

あなたのコードの一部を追加して、私があなたを助けることができますか?私はhttp://phpfiddle.org/でコードを実行しています。 – cwalsh

+0

このエラーを出力します。 非オブジェクト上のメンバー関数format()を呼び出す $ HyphenDate = str_replace( '。'、 ' - '、$ testDateChange); $ date = DateTime :: createFromFormat( 'd.m.Y H:i:s'、$ HyphenDate); \t \t \t echo $ date-> format( 'Y-m-d h:i:s'); \t \t \t echo $ date; – user3701575

-1

小数点を削除することでこれを解決しましたが、オブジェクトの誤用も発見しました。