php convert date value preferred to mysql date - php

I have get data from oracle database and date value kept on 27-MAY-09. I need to insert this value to mysql database via PHP. I need to convert date format as 2009-05-27.
Any one know about it please let me know correct php statement for do this.

use date() function
$date = '27-MAY-09';
$newData = date('Y-m-d', strtotime($date));
php fiddle

$date = DateTime::createFromFormat('j-M-y', $inputDate);
$newDate = $date->format('Y-m-d');
PHP 5.3 not earlier.

try this
$date1 = "27-MAY-09";
$data2 = date("Y-m-d",strtotime($date1));

Related

Read date value from import Excel in PHP

I've problem with read date value from import value (Excel) to mysql database. i've code as below :
$dataexcel[$i-3]['date_edit'] = date('m/d/Y', strtotime(trim($data['cells'][$i][25])));
i tried use strtotimeto define value from excel, but i've problem to save it into database, if my value in excel with excel date format is 1/1/2018 (it read as 1 Jan 2018, English time format), after i used my code above, it saved become 1970-01-01 it means 1 Jan 1970.
in another example,
i tried another input with date 2/1/2018, it saved into database as 2018-02-02.
from my samples above, in first there missmatch problem with year, then and in the 2nd sample missmatch problem come from date,
so how to declare date to solve my problem, if i want to save date format with simple way?
if there any advice , please, thanks...
From your comment answer, you can modification some code like below :
$date = str_replace("/", ".", $data['cells'][$i][25]);
$dates = date("Y-m-j", strtotime($date));
$dataexcel[$i-3]['date_edit'] = $dates;
$date still pick up from value without used strotime, so date format didn't read correctly...
You can convert the excel date to mysql date like below I did.
//Your input date
$input_date = $sheet->getCellByColumnAndRow(1,$i)->getValue();
$excel_date = $input_date; //here is that excel value 41621 or 41631
//Convert excel date to mysql db date
$unix_date = ($excel_date - 25569) * 86400;
$excel_date = 25569 + ($unix_date / 86400);
$unix_date = ($excel_date - 25569) * 86400;
//echo gmdate("Y-m-d", $unix_date);
//Insert below to sql
$added_date = gmdate("Y-m-d", $unix_date);
A simple function will do this thing
$date = str_replace("/", ".", $data['cells'][$i][25]);// replace the / with .
$date = date("Y-m-d", strtotime($date));
$dataexcel[$i-3]['date_edit'] = $date;
And in the database the default date format is YYYY-MM-DD , so it save correctly to the db
keep it simple. use a defined dateformat when reading the file (the dateformat can be provided as a parameter in case it needs to be adjustable). then, read the date in the following way instead of just using strtotime (taken from php.net's example):
$date = DateTime::createFromFormat('j-M-Y', '15-Feb-2009');
relevant example to your code, using the previous $date var:
$dataexcel[$i-3]['date_edit'] = $date->format("y-M-Y");
more info - http://php.net/manual/en/datetime.createfromformat.php

Incorrect value of datetime PHP and MySQL

In my site, I have a bootstrap datepicker which allows user to pick date in format of MM/DD/YYYY (e.g: 05/12/2014). Then when this data is submitted, I used the following PHP code to convert it into Datetime type, then insert into start_date (DATETIME datatype) column in MySQL .
$start_date = date('Y-m-d', $_POST['start_date']);
the insert query in PHP does nothing with reformatting the date. It just simply insert into corresponding column.
However, instead of inserting '2014-05-12', the value inserted into database is '1970-01-01'. That's so weird to me. Can anybody tell me what's wrong here. Is this that I used incorrect PHP function or incorrect timezone setting or ...
Just do this:
$start_date = date('Y-m-d', strtotime($_POST['start_date']));
You could also use strtotime() on your $_POST.
$start_date = date('Y-m-d', strtotime('05/12/2014'));
try to use
$date = str_replace('/', '-', $_POST['start_date']);
$start_date = date('Y-m-d', strtotime($date));
For more :- Converting between illogically formatted dates (changing /slash/ to -dash- )

Format mySQL timestamp by changing date-month and remove seconds using PHP function?

I echo $istoriko['timestamp'] from mySQL and I get 2014-04-24 00:10:29
How can I change it to
24/04/14 (or 2014) 00:10
Thank you
Try this, its simple
You can set any date format now in date functions
echo date("d/m/y",strtotime($istoriko['timestamp']));
Follow functions can help you:
$date = '2014-04-24 00:10:29';
$date = date('d/m/Y H:i',strtotime($date));
echo $date

DateTime defaulting to 1970-01-01

I am writing a PHP form for my website. The HTML side asks the user for a date which they enter in MM/DD/YYYY format. When that string is sent to PHP the following code changes it to the form that MySQL will recognize
$date = $_POST['date'];
$sqldate = date('Y-m-d', strtotime($date));
However when that date is entered into my MySQL database it is entered as 1970-01-01 and I can't figure out why.
NOTE: If I echo $sqldate I get the error Use of undefined constant sqldate - assumed 'sqldate' in C:/MYDIRECTORY
How about this?
//build a date
$date = date_parse_from_format("m/d/Y", $_POST["date"]);
//output the bits
$sqldate = "$date[year]-$date[month]-$date[day]";
A Unix timestamp is the number of seconds since 1970-01-01 so your call to strtotime() is returning 0.
How is DateTime working for you?
<?php
$dateStr = '08/18/2014';//$_POST['date'];
$dateTime = new DateTime($dateStr);
echo $dateTime->format('Y-m-d');
You're writing that the date format of the form is MM/DD/YYYY. Do you validate the input values to be sure that the format is always given?
THis worked for me
$input_date= trim($_POST["input_date"]);
$strtotime= strtotime($input_date);
$date_format= date('Y-m-d',$strtotime);

i want to get only date from datetime variable in php

I need to get only date from datetime variable. I was using this code. It was working fine but now I am facing problem when I'm trying to store only date in mysql please. Take a look at the code below and please tell me what I'm missing,
$date_time = "11-Dec-13 8:05:44 AM"
From the date I got from user input, I need to save only date in att_date variable.
$arr = explode("/", $date_time);
$arr2 = explode(" ", $arr[2]);
$att_date = $arr2[0].'-'.$arr[0].'-'.$arr[1];
Here is the solution:
$date_time = "11-Dec-13 8:05:44 AM";
$new_date = date("Y-m-d H:i:s",strtotime($date_time));
Even if you just use "Y-m-d" it will work instead of using "Y-m-d H:i:s",please practice below example:
$datetime = '2022-10-11 06:38:02';
$getOnlyDate = date('Y-m-d',strtotime($datetime));
echo $getOnlyDate; // output: 2022-10-11
You can get the date from the datetime variable using the
Date();
function from mysql using php query, it automatically extracts the date from it. You can learn it here.

Categories