2017-05-28 120 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文件中的日期,我需要的PHP把它转换为是YMD小时比较合适的格式:I:■

格式我需要转换 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

输出此错误, 在非对象上调用成员函数格式() $ 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

通过删除小数解决了这一问题,但也发现了该对象的误用。