This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I'm trying to upload a csv file to my mysql table with php.
I heard about the inline command but the problem is that the csv date format is dd/mm/yyyy and the mysql format is yyyy-mm-dd.
I tried almost every Date convert function but it doesn't worked out. always see 0000-00-00 on the Mysql Table.
How can I convert it correctly?
This is the last thing I tried:
($date[0] is the date cell in the csv. - 26/05/2016, and the second column in the mysql is the date)
while ($data = fgetcsv($handle, 1000, ","))
{
$date = strtotime($data[0]);
$newDate = date("d-m-Y", strtotime($data[0]));
$trydate = date("Y-m-d", strtotime($newDate));
echo $newDate . "<br>". $date. "<br>" . $data[0] . "<br>". $trydate. "<br>";
$import="INSERT INTO `testresult` (cityid, date, testnum,result) VALUES('$data[1]','$newDate','$data[3]','$data[2]')";
mysqli_query($con,$import) or die(mysql_error());
}
The output was:
01-01-1970
(blank)
26/05/2015
1970-01-01
strtotime waiting an input an expected format.
You need to split your string, and create a Y-m-d fromat from it, or you can use the DateTime object.
$dateString = '11/05/2016';
$Date = DateTime::createFromFormat('d/m/Y', $dateString, new DateTimeZone(('UTC')));
echo $Date->format('Y-m-d');
OUTPUT
2016-05-11
With strtotime
$string = '26/05/2016';
$trydate = date("Y-m-d", strtotime(substr($string, -4) . "-" . substr($string, 3, 2) . "-" . substr($string, 0, 2)));
echo $trydate;
Result are the same.
I believe you need to convert the / to - so that strtotime understands the format..
for example
while(...) {
$date = date('Y-m-d', strtotime(str_replace('/', '-', $data[0])));
echo $date;
$import = "...";
}
You need to use the SET clause, along with a variable to reference the contents of the row at that column. In your column list, you assign your date column to a variable name. You can then use it in your SET statement.
LOAD DATA LOCAL INFILE 'file.csv' INTO TABLE testresult
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
(cityid, #var1, testnum,result)
set dateOfBirth = STR_TO_DATE(#var1, '%d/%m/%y')
Related
This question already has answers here:
Convert influxdb time format to ISO8601 format
(4 answers)
Closed last year.
With a php app, I have a date like this '2019-07-22T04:44:43.843710438Z' and I can't figure out how to convert it to a date like this 7/22/2019 3:11 PM. strtotime just returns false, I tried a few time zones and nothing works. Here is my scratch pad of stuff I've been trying
$timestamp = '2019-07-22T04:44:43.843710438Z';
//$reset = date_default_timezone_get();
//date_default_timezone_set('America/New_York');
//$stamp = strtotime($timestamp);
//date_default_timezone_set($reset);
$result = date_parse_from_format("j F Y G:i", $timestamp);
You'll have a much easier time (couldn't resist) working with the DateTime class (https://www.php.net/manual/en/class.datetime.php).
You're running into issues because the value you have isn't a timestamp so you can't use it like one.
The original time value uses nanosecond precision (9dp) which isn't supported by PHP. In order to use that time string with DateTime, you'll first have to truncate it to microseconds (6dp). It then has a method that will return your value in the desired format (https://www.php.net/manual/en/function.date.php).
$time = '2019-07-22T04:44:43.843710438Z';
$time_micro_precision = substr($time, 0, -4) . 'Z'; // drop the last three digits to move from nanoseconds to microseconds.
$datetime = new DateTime($time_micro_precision);
echo $datetime->format('j/n/Y g:i A');
Using strtotime works if you restrict the timestamp to 2 decimal places. Try the following:
$time = "2019-07-22T04:44:43.843710438Z";
$temp_str = rtrim($time, "Z");
$temp_arr = explode(":", $temp_str);
$temp_str = number_format($temp_arr[count($temp_arr)-1], 8);
$time = $temp_arr[0] . ":" . $temp_arr[1] . ":" . $temp_str . "Z";
$timestamp = strtotime($time);
echo date("m/d/Y H:i a", $timestamp);
I am having issues getting my code to return the correct response.
$Birthd = '06-27-1996';
$NewISSdate = date("m-d-Y", strtotime(date("m-d-Y", strtotime($Birthd)) . " +21 years"));
When I run this the response is: "12-31-1969"
I believe this is a default date of sorts, but what can I do to repair my code? If ran with a different $Birthd string such as "07-03-1996".
You need to change string date format and then add years to it like below:-
$Birthd = '06-27-1996';
$Birthd = str_replace("-","/",$Birthd);
echo $NewISSdate = date("Y-m-d", strtotime(date("Y-m-d", strtotime($Birthd)) . " + 21 years"));
Output:-https://eval.in/835509 OR https://eval.in/835555
Reference:- php different acceptable date formats
Change - to / and try...
$Birthd = '06/27/1996';
$NewISSdate = date("m/d/Y", strtotime(date("m/d/Y", strtotime($Birthd)) . " +21 years"));
I have 2 variables stored into mysql:
campaign_date Format: d/m/Y
campaign_time Format: 24Hr
How could I concatenate them into one single variable like this:
2015-06-16T18:30
I tried with:
$new_datetime=$campaign_date.'T'.$campaign_time;
But it's not working
This should work for you:
(For the first date you have to change / to - so you can use date() and you can change the order of d/m/Y to Y-m-d, after that it's a simple concatenation with the time at the end)
<?php
$campaign_date = "16/12/2014"; //Data from DB
$campaign_time = "18:00"; //Data from DB
echo $new_datetime = date("Y-m-d", strtotime(str_replace("/", "-", $campaign_date))) . "T" . date("H:i", strtotime($campaign_time));
?>
Output:
2014-12-16T18:00
Try it,i tested it myself.
$db_date = date("Y-m-d",strtotime($db_date));
$db_time = date("h:i:s",strtotime($db_time));
echo $db_date.'T'.$db_time;
I am having some problems getting my date into my SQL table. I do not use datetime, but date.
This is the code I use, and the problem is that my SQL server doesn't recognize $date_add as a date and just puts the default 0000-00-00 in the date section...
if (isset($_POST['postbutton'])){
$articlepost = nl2br($_POST['article'])."<br>";
date_default_timezone_set('Europe/Oslo');
$datepic = date(YYYY-MM-DD);
$pictureurls = $_SESSION['urlpost'];
$thumbnail = 123;
$title = $_POST['title'];
$date_add = $datepic;
$articlepostimg = $articlepost.$pictureurls;
$insertpost = $db->prepare("INSERT INTO posts (title,post,date_add,thumbnail) VALUES (:title,:post,:date_add,:thumbnail)");
$insertpost->execute(array(':title' => $title, ':post' => $articlepostimg, ':date_add' => $date_add, ':thumbnail' => $thumbnail));
unset($_SESSION['urlpost']);
}
Here is what I see in my database after I submit my form:
Try the following:
$datepic = date("Y-m-d");
Here are the docs for date()
As for the question added in your comments, after you retrieve your date you'll need to do something like the following, where $orig_date is assigned the date retrieved from the database. As for converting it to Norwegian, I think you'd have to look into setlocale(), which I think warrants a different question.
$formatted_date = date('j, M Y', strtotime($orig_date));
You need to use either double quote or quotes to make date() function work propely
$datepic = date('YYYY-MM-DD');
This is just to add to already given answers after seeing OP asked for a language conversion in Norwegian, and by no means is it meant to step on anyone's feet, but as a complimentary answer.
You can use the following month conversion code which are in French, but you can easily modify it in Norwegian.
Notice that "Mars" is spelled the same way.
(This taken from my own code library)
<?php
// enter date format 2011-01-31 (Y-m-d)
function date_in_french ($date){
$week_name = array("Dimanche","Lundi","Mardi","Mercredi","Jeudi","Vendredi","Samedi");
$month_name=array("","Janvier","Février","Mars","Avril","Mai","Juin","Juillet","Août",
"Septembre","Octobre","Novembre","Décembre");
$split = preg_split('/-/', $date);
$year = $split[0];
$month = round($split[1]);
$day = round($split[2]);
$week_day = date("w", mktime(12, 0, 0, $month, $day, $year));
return $date_fr = $week_name[$week_day] .' '. $day .' '. $month_name[$month] .' '. $year;
}
$currentDate=date('Y-m-d');
echo "Current Date: ";
echo date('D')." ".date('d')." ".date('M')." ".date('Y');
echo "<br>";
echo "Date in French => ".date_in_french($currentDate);
?>
Hi I know this question is a bit trivial. I googled it but could not find the exact problem anywhere.
I have a string which contains a application filling date how can I convert this string into date
$appln_filling_date = '20020315';
The type of appln_filling_date is string I want to convert its type to date and want the data remain the same. The field in the database has a type date.
EDIT
This is what I am trying to do
$appln_filling_date = strtotime($appln_data['bibliographic-data']['application-reference']['document-id']['1']['date']['$']);
$appln_filling_date = date('Y-m-d', $appln_filling_date);
If your date is in some standard date format use this example:
$d = new DateTime('20020315');
echo $d->format('Y-m-d');
# or [if you do not have DateTime, which is in php >= 5.2.0]
$t = strtotime('20020315');
echo date('Y-m-d', $t);
If your date is not in some standard format use this example:
$d = DateTime::createFromFormat('d . Y - m', '15 . 2002 - 03');
echo $d->format('Y-m-d');
$date = DateTime::createFromFormat('Ymd', $appln_filling_date);
This worked for me like a charm
$appln_filling_date = $appln_data['bibliographic-data']['application-reference']['document-id']['1']['date']['$'];
$appln_filling_date = date_create(date('Y-m-d', $appln_filling_date));
Thanks for the down voting