Converting String to DateTime in PHP - php

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')";

Related

How to convert a Datetime string to standard mysql

I made a function in php to convert date and time coming from a txt to the mysql standard.
But she is turning the month wrong.
I have tried all these conversions but to no avail.
I would like your help because I don't know what else to do.
function convertstringdate('05/02/202116:43:49'){
$date = new DateTime($datetime);
return date_format($date, "Y-m-d H:i:s");
}
or
$input = '05/02/202116:43:49';
$date = strtotime($input);
echo date('Y-m-d H:i:s', $date);
Since you know the format of your datetime-string I suggest you use createFromFormat() like so:
$string = '05/02/202116:43:49';
$dateTime = DateTime::createFromFormat('d/m/YG:i:s', $string);
var_dump(date_format($dateTime, "Y-m-d H:i:s"));
// output: string(19) "2021-02-05 16:43:49"
Note that I am not sure if 05 or 02 is supposed to be the month in your example, so if this seems wrong to you you might just have to switch around d/m in the format string and make it m/d.
For explanation what character means what poriton of the datetime, see the linked above documentation reference.

Convert date and time to only year in php

I have a variable is which the value coming is Date along with time in php. How do I convert it into a variable to get only the year? I do not need automatic updation but the format change is needed. Normal answers are giving it about date but my variable is containing time as well.
The format coming by now is 2017-12-11 4:06:37 and i need only 2017
Use like this:
<?php echo date('Y',strtotime('now'));?>
You can you simple DateTime function and date_formate() function for displaying separate year, month and date.
For that you have to first convert in Object of your current Date time string by using :
$date = new \DateTime('2017-12-11 4:06:37');
And then you can use date format function by using below code:
echo date_format($date, "Y"); //for Display Year
echo date_format($date, "m"); //for Display Month
echo date_format($date, "d"); //for Display Date
You can code like this (working perfectly):
$format = 'Y-m-d H:i:s';
$date = DateTime::createFromFormat($format, '2009-02-15 15:16:17');
echo "Format: $format; " . $date->format('Y') . "\n";
As mentioned by Himanshu Upadhyay, this is correct and the easiest way.
<?php
echo date('Y',strtotime('now'));
?>
But i would recommend you to read this here. You should really do actually!
By using DateTime class
$date = new \DateTime('2017-12-11 4:06:37');
echo $date->format('Y');

timestamp to php from timestamp array

Hi guys i am really new to php and i am trying to convert the timestamp from an xml array but with no sucess , i read everything i found but still can find the way can you please help ?
I am using this code to decode an xml api output
$mysongs = simplexml_load_file('http://example.com/test/xml/');
$timestamp = $mysongs->item->timestamp;
$playtime = date("Y-d-m G-i-s",$timestamp);
If i echo $timestamp it works fine, $playtime doesn't...
Tried with :
echo gmdate("Y-m-d", $timestamp);
&
echo date('Y-m-d H:i:s', $wra);
&
echo '<timeplayed>' . date('Y-m-d G:i:s', $mysongs->item->timestamp) . '</timeplayed>';
Still no luck.. Time is not showing.. If i use this example
echo '<timeplayed>' . date('Y-m-d G:i:s', (1365532902)) . '</timeplayed>';
it works fine.. What am i doing wrong here ?
Update
Finally i found it.. it needed to cast the $timestamp as integer for the date to decode properly as euxneks sugested..
So right code should be
$timestamp = intVal ($mysongs->item->timestamp);
and then
$playtime = date("Y-d-m H-i-s",($timestamp));
& finally echo ($playtime); and it works fine...
Thanks everyone for your replys problem solved
It's likely that you need to cast your result as an integer (it's been a while since I've used simplexml but I'm pretty sure it doesn't automatically type the values received):
http://php.net/intval
Also, strtotime might work too :)
http://php.net/strtotime
You are definitely missing strtotime function, here is an example:
$str = '04/09/2013';
$date = date('Y-m-d H:i:s',strtotime($str));
echo $date;
This will output:
2013-04-09 00:00:00
Updated
Since strtotime is alread used, what about using:
$date = date('Y-m-d H:i:s', ($timestamp));

php convert date value preferred to mysql date

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));

problems getting values from query string

My below function that is copied from another function that works fine. Should get values from the query string and turn them into a date:
function updateShift()
{
echo $name = $_GET['shift_name'];
echo $start_date = date('Y-m-d H:i:s', strtotime("{$_GET['start_hours']}:{$_GET['start_minutes']} {$_GET['start_ampm']}"));
echo $stop_date = date('Y-m-d H:i:s', strtotime("{$_GET['stop_hours']}:{$_GET['stop_minutes']} {$_GET['stop_ampm']}"));
}
However it returns:
Shift Name
1969-12-31 17:00:00
1969-12-31 17:00:00
Any idea why this works fine elsewhere but not here? The query string is there as evidenced by the shift_name coming through correctly.
What if you do this:
function updateShift()
{
echo $name = $_GET['shift_name'];
echo $start_date = date('Y-m-d H:i:s', strtotime($_GET['start_hours'].':'.$_GET['start_minutes'].' '.$_GET['start_ampm']));
echo $stop_date = date('Y-m-d H:i:s', strtotime($_GET['stop_hours'].':'.$_GET['stop_minutes'].' '.$_GET['stop_ampm']));
}
or
function updateShift()
{
echo $_GET['shift_name'];
echo date('Y-m-d H:i:s', strtotime($_GET['start_hours'].':'.$_GET['start_minutes'].' '.$_GET['start_ampm']));
echo date('Y-m-d H:i:s', strtotime($_GET['stop_hours'].':'.$_GET['stop_minutes'].' '.$_GET['stop_ampm']));
}
The dates you received were the start of the Unix epoch since your date function call was getting false (or 0) as the second argument. I just ran some quick tests using your code and I am seeing that strtotime is returning false with the values supplied.
echo "{$_GET['start_hours']}:{$_GET['start_minutes']} {$_GET['start_ampm']}";
=> '4:0 PM'
You need to make sure you have 2 digits in your minutes field to allow strtotime to see it as a valid time and to parse it correctly. To to this you can either update your query string, use str_pad or sprintf to ensure you have the 2 digits required for the time to be valid.

Categories