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.
Related
I'm use this code:
$my_date_time = DateTime::createFromFormat('m/d/Y H:i', '20/02/2018 00:51')->format('Y-m-d H:i');
echo $my_date_time;
the code should show me this: 2018-02-20 00:51
but show: 2019-02-20 00:51
it increase 1 more year.. why..?
I think you reversed the day and the month when using DateTime::createFromFormat.
Try this format:
d/m/Y
$my_date_time = DateTime::createFromFormat('d/m/Y H:i', '20/02/2018 00:51')->format('Y-m-d H:i'); echo $my_date_time;
Test online
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 using datetimepicker for user input. The format is: mm-dd-yy hh:mm tt (Example: 08-25-2014 01:49 pm). I need to convert it to MySQL datetime format using php (Example of same time properly converted 2014-08-25 13:49:00) for MySQL storage.
<?php
$test = '08-25-2014 01:49 pm';
$test = str_replace("-","/","$test");
$test = new DateTime("$test");
$test = date_format($test, 'Y-m-d H:i:s'); // 2011-07-01 00:00:00
?>
Update: For some reason I had to remove the - and replace it with /. Any idea why forward slashes are fine but not -?
As you are using an American date format and the - seperator which is used for NON US date formats, you will probably have to actually do something like this :-
$date = '08-25-2014 01:49 pm';
$dt = new DateTime();
$dt->createFromFormat('m-d-Y h:i a', $date);
$date = $dt->format('y-m-d');
You can do this using the DateTime class:
$mydate = new DateTime("08-25-2014 01:49 pm");
$MySQL_format = $mydate->format("m-d-Y h:i a");
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 ;)
I have a string like 2012-11-08.
I want to convert this string into php date format with time too.
The output should be like
$smv = date('Y-m-d H:i:s', time());
print $smv // 2012-11-08 16:05:56 (If we consider India )
I tried
$day = '2012-10-08';
echo = date("Y-m-d H:i:s",strtotime($day));
But it is printing the date as 2012-10-08 00:00:00 (current time is not printed).
Try like this
$day = '2012-10-08'.date('H:i:s', time());
echo date("Y-m-d H:i:s",strtotime($day));