I've got an datetime string like this: 28-06-14 11:01:00
That's European for day 28, month 6, year 2014...
I'm trying to convert it to 2014-06-28 11:01:00 so that I can insert it into a database with field type datetime.
I've tried multiple things like this:
$datumHolder = new DateTime($data['datum'], new DateTimeZone('Europe/Amsterdam'));
$datum1 = $datumHolder -> format("Y-m-d H:i:s");
$datum2 = date( 'Y-m-d', strtotime(str_replace('-', '/', $data['datum']) ) );
$datum3 = DateTime::createFromFormat( 'Y-m-d-:Hi:s', $data['datum']);
This is the output I get:
datum1: 2028-06-14 11:01:00
datum2: 1970-01-01
And I get an error for datum3:
echo "datum3: " . $datum3->format( 'Y-m-d H:i:s'); . '<br />';
Call to a member function format() on a non-object
What am I doing wrong and how do I get this to work?
Your $datum3 method is correct way, you just have invalid input format.
Use:
$datum3 = DateTime::createFromFormat('d-m-y H:i:s', $data['datum']);
// $data['datum'] is '28-06-14 11:01:00'
$datetime = DateTime::createFromFormat('d-m-y H:i:s', $data['datum']);
// y is two digit representation of a year, while Y is full numeric representation of a year, 4 digits
echo $datetime->format('Y-m-d H:i:s');
$timestamp = strtotime('28-06-14 11:01:00');
$output = gmdate("Y-m-d H:i:s", $timestamp);
echo $output;
That will give you a hint or two ;)
Related
I am working on following issue in PHP.
I have these variables:
$fecha = "01-07-2017";
$hora = "11:30";
Now I want to create a date variable with format Y-m-d H:i.
For that I am using following code:
$newDate = date("Y-m-d H:i", strtotime($fecha.' '.$hora));
But I am getting the output:
echo $newDate = 2017-01-07 11:30
I need it to be: 2017-07-01 11:30
What am I doing wrong?
Use the DateTime API to parse your string(s) initially
$dt = DateTime::createFromFormat('d-m-Y H:i', $fecha.' '.$hora);
$newDt = $dt->format('Y-m-d H:i');
Demo ~ https://eval.in/827662
Change your code to:
$newDate = date("Y-d-m H:i", strtotime($fecha.' '.$hora));
Notice that I just switched around 'h' & 'd' in the date function.
I am trying to format a date as 2015-07-12 15:00 from the values declared in my variables
// unix
$date = 1436713200
// string
$time = '15:00';
to get a date format 2015-07-12 15:00 but failing, using this
$newdate = date('Y-m-d H:i:s', $date.' '.$time);
I get 'A non well formed numeric value encountered'. Can anyone help? I understand it is possibly due to the mix of string and unix but unsure how to get round this.
I would suggest you to use DateTime instance to avoid timezone issues:
$d = date_create('#1436713200'); // creates DateTime instance
$d->setTime(15, 00); // sets current time to desired hours, minutes
echo $d->format('Y-m-d H:i:s'); // prints it out with format specified
//⇒ 2015-07-12 15:00:00
You do not have to provie the $time variable. Unix time is a full date with time.
Use:
$newdate = date('Y-m-d H:i:s', $date);
Use this
$date = date('Y-m-d','1436713200');
// string
$time = '15:00';
echo $newdate = date('Y-m-d H:i', strtotime($date.' '.$time));
I'm receiving some data from an HTTP POST which includes what is labelled a GMT Timestamp:
<gmt_timestamp>201308031525</gmt_timestamp>
I then need to take this timestamp and convert it to this format:
MM/DD/YYYY HH:MM
So far I've been trying this:
$ts = $_GET['timestamp'];
$date = DateTime::createFromFormat('ymdHi', $ts);
$fmTimestamp = $date->format('m/d/Y h:i:s A');
but that generates a "PHP Fatal error: Call to a member function format() on a non-object" for the 2nd line. Any idea what I'm doing wrong?
You have a bug in this line:
$date = DateTime::createFromFormat('ymdHi', $ts);
You need an uppercase Y for the year:
$date = DateTime::createFromFormat('YmdHi', $ts);
A lowercase y indicates "A two digit representation of a year", whereas you need Y ("A full numeric representation of a year, 4 digits"). See the docs here.
You also need to set the timezone before you begin:
date_default_timezone_set('UTC');
(PHP does have a GMT timezone, but it shouldn't be used. UTC behaves the same as GMT within PHP.)
Edit
To get your desired output format of:
MM/DD/YYYY HH:MM
you need to do:
$fmTimestamp = $date->format('m/d/Y H:i');
Also, since you're "receiving some data from an HTTP POST", you need to use $_POST instead of $_GET:
$ts = $_POST['timestamp'];
So the complete code is:
date_default_timezone_set('UTC');
$ts = $_POST['timestamp'];
$date = DateTime::createFromFormat('YmdHi', $ts);
$fmTimestamp = $date->format('m/d/Y H:i');
Keep it simple stupid.
$input = $_GET['timestamp']; // 201308031525
$year = (int)substr($input,0,4);
$month = (int)substr($input,4,2);
$date = (int)substr($input,6,2);
$hour = (int)substr($input,8,2);
$minute = (int)substr($input,10);
$date_obj = new DateTime($year . '-' . $month . '-' . $date .' ' . $hour . ':' . $minute);
echo $date_obj->format('m/d/Y h:i:s A');
and the output is:
08/03/2013 03:25:00 PM
You are not instantiating the object you are trying to use.
Try this approach instead:
$date = new DateTime;
$date->createFromFormat('ymdHi', $ts);
$fmTimestamp = $date->format('m/d/Y h:i:s A');
This is untested, just saying ...
I have a table with column date set to datetime. When i return and get the date from row it is only returning the date and not the time.
$date = $row["date"];
I have tried to format as below and I get the error of:
Warning: date_format() expects parameter 1 to be DateTime, string given
$date = date_format($row["date"], 'Y-m-d H:i:s');
How do I get the whole value?
in your select statement, cast the date into datetime. ex
SELECT CAST(date AS DATETIME) newDate
and retrieve it as
$dateTime = strtotime($row["newDate"]);
try:
$date = date_format(new DateTime($row['date']), "Y-m-d H:i:s");
OR
$date = date('Y-m-d H:i:s', strtotime($row['date']));
You need to convert your string $date to the right type. I had to try a few things to get this right, but this now behaves on my machine:
$thisDate = "2013-02-02 22:17:06"; // example you gave, as a string
$timezone="America/New_York"; // machine was complaining when I didn't specify
$DT = new DateTime($thisDate, new DateTimeZone($timezone)); // this really is a DateTime object
echo $DT->format('Y-m-d H:i'); // you can echo this to the output
$dateString = $DT->format('Y-m-d H:i:s'); // or format it into a string variable
You need to convert the string to date type first. Then date_format() will work. Try the following.
$date = date_format(date_create($row["date"]), 'Y-m-d H:i:s');
Good Luck
I have a forum in PHP which takes a date like in the form
dd/mm/yyyy hh:mm:ss. However, I need to insert it for SQL as a DATETIME in the format as yyyy-mm-dd hh:mm:ss. How can I convert this data?
Your date time format is wrong: dd/mm/yyyy hh:mm:ss. Probably you mean d/m/Y H:i:s
If you have 5.3+ version there is safe way to convert the date time into another format. Here's an example:
$timestamp = '31/05/2001 12:22:56';
$timestamp = DateTime::createFromFormat('d/m/Y H:i:s', $timestamp);
echo $timestamp->format('Y-m-d H:i:s');
or if you like more procedural way:
$timestamp = '31/05/2001 12:22:56';
$timestamp = date_create_from_format('d/m/Y H:i:s', $timestamp);
echo date_format($timestamp, 'Y-m-d H:i:s');
Be careful with previous suggestions. Some are completely wrong and others could lead to errors.
You can use the strtotime and date to rework the format.
$new_date = date( "Y-m-d H:i:s", strtotime( $old_date ) );
What this does is take your old date (dd/mm/yyyy hh:mm:ss), converts it to a unix timestamp that can then be used with the php date function to format the date to the desired format.
Two of several possible ways:
Convert in code and then pass the converted value to mysql: $mysqldate = date( 'Y-m-d H:i:s', $phpdate );
Let mysql do the work by using its built-in functions:
$query = "UPDATE table SET datetimefield = FROM_UNIXTIME($phpdate) ...";
if you have datetime avaialable from a from like above format then u just need to use following function.
function localToMysql($dateTime){
$date_chunks = explode('/', $dateTime);
$time_chunks = explode(' ', $date_chunks[2]);
$final_format = $time_chunks[0] . "-" . $date_chunks[1] . "-" . $date_chunks[0] . " " . $time_chunks[1];
return $final_format;
}