I am having problems converting a string into datetime format in order to store variable into a datetime-type field of a sql table
value of $timestamp:
2019-02-23T08:30:03.77
$datum2 = substr($timestamp,0,19);
$datum2 = str_replace('T',' ', $datum2);
echo $datum2 ."<br>";
--> 2019-02-23 08:30:03 .... echo output looks ok to me
$datum2 = date_format($datum2,'Y-m-d H:i:s');
--> error message
Warning: date_format() expects parameter 1 to be DateTimeInterface
Thank you for any hints
Stefan
Check below code:
$date = '2019-02-23 08:30:03';
$datum2 = date('Y-m-d H:i:s', strtotime($date));
You can convert string date to timestamp and later can change to date format. Hope it helps you.
You have to use date_create function on $timestamp:
$datum2 = date_format(date_create($timestamp),'Y-m-d H:i:s');
Related
Getting DateTime
Hi I am not good on programming so don't judge so strong, I want to get date and time from fields.
$date_start = $this->input->post('date_start');
$time_start = $this->input->post('time_start');
$date_end = $this->input->post('date_end');
$time_end = $this->input->post('time_end');
$data['start'] = date_format('U = Y-m-d H:i:s', $date_start.' '.$time_start);
$data['end'] = date_format('U = Y-m-d H:i:s', $date_end.' '.$time_end);
Error
date_format() expects parameter 1 to be DateTimeInterface, string given
Try like this
$data['start'] = date_format(date_create( $date_start.' '.$time_start),"U = Y-m-d H:i:s");
The function date_format expects an object implementing DateTimeInterface as the first argument and the format string as the second argument.
You can create a DateTime object from a string with it's factory method DateTime::createFromFormat also procedurally with date_create_from_format documented on the same page.
I'm searching for the datetime format string to parse this format:
2015-09-24T07:46:13.722Z
I have tried:
var_dump(DateTime::createFromFormat('Y-m-d\TG:i:suT', '2015-09-24T07:46:13.722Z'));
var_dump(DateTime::createFromFormat(DateTime::W3C, '2015-09-24T07:46:13.722Z'));
Both return false what is the stupid little error?
Using DateTime()
We can rewrite the above code using DateTime() as so:
$date = new DateTime('2015-09-24T07:46:13.722Z');
$new_date_format = $date->format('Y-m-d H:i:s');
Try using strtotime() with date() like this
$a = "2015-09-24T07:46:13.722Z";
echo date("Y-m-d\TG:i:suT",strtotime($a));
// output is
//2015-09-24T9:46:13000000CEST
for further info check this link
I have a problem by converting a date in proper format.
I get the time the from Facebook API in this format: 2013-08-23T09:00:00
I then $fbdate = date('2013-08-23T09:00:00');
When I echo $fbdate, it retuns 2013-08-25UTC05:00:00.
Then I tried:
$datum = date("d.m.Y",$fbdate);
$uhrzeit = date("H:i",$fbdate);
To extract the date and the time but it always returns:
01.01.1970 for $datum and 00:33 for $uhrzeit.
You should use strtotime() to parse a date string into a UNIX timestamp:
$fbdate = strtotime('2013-08-23T09:00:00');
$datum = date('d.m.Y', $fbdate);
$uhrzeit = date('H:i', $fbdate);
Try using the DateTime class:
$fbdate = '2013-08-23T09:00:00';
$date = DateTime::createFromFormat('Y-m-d\TH:i:s', $fbdate);
$datum = $date->format('d.m.Y');
$uhrzeit = $date->format('H:i');
echo $datum;
echo $uhrzeit;
$datum = date("d.m.Y",strtotime($fbdate));
$uhrzeit = date("H:i",strtotime($fbdate));
The date function in PHP is meant to convert a microtime into a date, so you need to convert your string dates to microtimes first.
Have you tried this? You should use strtotime() when using the date function.
$datum = date("d.m.Y", strtotime($fbdate));
$uhrzeit = date("H:i", strtotime($fbdate));
Use strtotime()
$a = date("Y-M-d", strtotime($datum));
echo $a.$uhrzeit;
More info http://php.net/manual/en/function.strtotime.php
I'm not a huge fan of using timestamp.
What I did to solve this issue was:
$updatedDate = new DateTime(
preg_replace('/^(.*)\+0000$/', '$1', $fbUser->getProperty("updated_time")),
new DateTimeZone("UTC")
);
It chops the +0000 part and creates the DateTime object with an explicit UTC timezone.
I need to insert the datetime value entered from the HTML form using PHP into the MySQL database. However I receive the Incorrect datetime value error each time I try to execute the code below,
$rosterstartdate=$_GET['rosterstartdate'];
$rosterenddate=$_GET['rosterenddate'];
//$date = date_create_from_format('d/M/Y:H:i:s', $rosterstartdate);
//$date->getTimestamp();
//echo $date;
$date = strtotime($rosterstartdate);
echo date('d/M/Y H:i:s', $date);
// echo DATE_FORMAT($rosterstartdate,"%Y%m%d %H%i%s");
$con=mysql_connect("localhost","root","");
if($con==true){
mysql_select_db("attendance_db",$con);
$query="insert into tblroster values(LAST_INSERT_ID(),'$rosterteam','$rostershifts','$date','$rosterenddate')";
I have tried using each of the different techniques above to do the conversion but it does not work. Any ideas on how this could be inserted
try this:
$date = date('Y-m-d H:i:s', $date);
Instead of echoing it out, use that code to format the date.
However, it looks like what you really want is this:
$rosterstartdate = date('Y-m-d H:i:s', strtotime($rosterstartdate));
$rosterenddate = date('Y-m-d H:i:s', strtotime($rosterenddate));
This way you can just reference those two variables in your query string.
You don't need to format it if you have a unixtime use FROM_UNIXTIME,
change your query as
$rosterstartdate=$_GET['rosterstartdate'];
$rosterenddate=$_GET['rosterenddate'];
//$date = date_create_from_format('d/M/Y:H:i:s', $rosterstartdate);
//$date->getTimestamp();
//echo $date;
$date = strtotime($rosterstartdate);
$con=mysql_connect("localhost","root","");
if($con==true){
mysql_select_db("attendance_db",$con);
$query="insert into tblroster values(LAST_INSERT_ID(),'$rosterteam','$rostershifts',FROM_UNIXTIME($date),'$rosterenddate')";
I have a date/time string like this: 180510_112440 in this format ddmmyy_hhmmss
I need a snippet for having a string formatted like this way: 2010-05-18 11:24:40
Thanks for help.
another possible answer is the common use of strptime to parse your date and the mktime function:
<?php
$orig_date = "180510_112440";
// Parse our date in order to retrieve in an array date's day, month, etc.
$parsed_date = strptime($orig_date, "%d%m%y_%H%M%S");
// Make a unix timestamp of this parsed date:
$nice_date = mktime($parsed_date['tm_hour'],
$parsed_date['tm_min'],
$parsed_date['tm_sec'],
$parsed_date['tm_mon'] + 1,
$parsed_date['tm_mday'],
$parsed_date['tm_year'] + 1900);
// Verify the conversion:
echo $orig_date . "\n";
echo date('d/m/y H:i:s', $nice_date);
$inDate = '180510_112440';
$date = strtotime('20'.substr($inDate,4,2).'-'.
substr($inDate,2,2).'-'.
substr($inDate,0,2).' '.
substr($inDate,7,2).':'.
substr($inDate,9,2).':'.
substr($inDate,11,2));
echo date('d-M-Y H:i:s',$date);
Assumes date will always be in exactly the same format, and always 21st century
list($d,$m,$y,$h,$i,$s)=sscanf("180510_112440","%2c%2c%2c_%2c%2c%2c");
echo "20$y-$m-$d $h:$i:$s";