PHP Converting string date and time to MySQL DateTime [duplicate] - php

This question already has answers here:
mysql converting text input to datetime field
(5 answers)
Closed 1 year ago.
I have two text fields, one for the date in 08/01/2012 format and a second field containing the time. I currently have the time field in the format 09:41am but I have some flexibility with it's format (if 24hr is easier for example).
I was planning on just concatenating the strings and then converting. Should I convert the date to 2012-08-01 first?
How can I end up converting to datetime (2012-08-01 09:41:00)? How to convert back out of it into a 08/01/2012 and 09:41am format?

SELECT STR_TO_DATE(concat('08/01/2012', '09:41am'),'%d/%m/%Y%h:%i');

On the database side, you can use:
STR_TO_DATE() using the specifiers in this table to convert into a database friendly format. Reference
DATE_FORMAT() will then return whatever part you want of that database time. Reference
On the PHP side you can use:
strtotime() - Parse about any English textual datetime description into a Unix timestamp
date - Format a local time/date
strotime will produce a Unix timestamp based on any string date/time that is passed to it. If you pass both your fields (08/01/2012 & 09:41am) it will produce a timestamp based on it. To reverse the process, you use date("m/d/Y H:ia").
$field1 = '08/01/2012';
$field2 = '09:41am';
$stamp = strtotime($field1 . ' ' . $field2);
echo date("m/d/Y H:ia", $stamp);
I have not tested this, but it should work.

Related

php date & strtotime function resulting in 1970-00-00 00:00 value [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 5 years ago.
As ive echoed out the datetime value from my db, i am now trying to display this value to edit it in the datetime-local field within my form.
The datetime vaue from db is set to:
22/3/2017 10:00:00
however, after attempting to use the following code, im left with this:
1970-01-01T01:00:00
$dat = date("Y-m-d\TH:i:s", strtotime($_GET["dat"]));
How & why is this function not working correctly to display '22/3/2017 10:00' in the form field?
Use DateTime::createFromFormat:
$date = DateTime::createFromFormat('d/m/Y H:i:s', '22/3/2017 10:00:00');
$dat = $date->format('Y-m-d\TH:i:s');
echo $dat;
Your code is not working because strtotime makes assumption based on delimiters about actual format:
m/d/Y- American format
d.m.Y or d-m-Y - European
It's not working because strtotime() can translate only specific date format.
Check the manual. For a list of supported date format, look here.
Your date format looks not included in the supported ones to me.
Examples:
strtotime("03/22/2017 10:00:00"); // Works: returns 1490173200
strtotime("22/3/2017 10:00"); // Doesn't work: returns false
You have to either change the date format in your DB or format it to one of the supported formats to make it work.

Formatting the time hh:mm:ss.0000000 to hh:mm not working

I am using MsSQL in PHP. I am storing the time in database table row with datetime as datatype.The time is stored like this :08:30:00.0000000.I need the time to be displayed in 08:30.I have used
date('h:i', $time_value); // $time_value stores the time value
This formats the date and gives the result in 4:00. Any formatting is required to display the correct time stored in database?
The PHP date function does not expect a Mysql datetime as second parameter but you do so:
date('h:i', $time_value);
^
|
second parameter
This is the reason why it does not work. You're using the wrong value, convert the database value into a timestamp first because the date function needs a timestamp. As you migh imagine, this has been done before, here is just a selection of related Q&A material:
MySQL convert datetime to unixtime?
MySQL convert datetime to Unix timestamp
Alternatively just use a string function like substr to obtain the string you're looking for:
$time_value = '08:30:00.0000000';
echo substr($time_value, 0, 5); # 08:30
Demo: https://eval.in/146148
You can do the conversion / formatting with the SQL statement already, a related example is given in:
How to display time in HH:MM format?
You can also use:
date('h:i', strtotime('08:30:00.0000000'));
In your case:
date('h:i', strtotime($time_value));

MYSQL mixing up dates

I am trying to insert a date in my Database which I get from a php input.
The code I am using to insert the value looks like this
$length = strrpos($fristdatum, " ");
$newDate = explode(".", substr($fristdatum, $length));
$fristdatum = $newDate[2] . "-" . $newDate[1] . "-" . $newDate[0];
Lets say I enter 14.12.2012 as the date if I echo $fristdatum I get 2012-12-14 but as soon as I insert it in my MySQL DB it turn to 2014.12.20 any ideas?
The Column Type is date. The insert is somewhat like this
mysql_query("INSERT INTO sch_anschreiben (date)values('$fristdatum'))
there are more values but I guess that doesn't matter
Thanks in Advance!
Well thanks for the help guys i figured it out i used $fristdatum in a array for str_replace ,after i formated it, like this
$patern = array("[Date]")
$words=array($fristdatum)
$content = str_replace($patern, $words, $content);
and after that inserted it in the DB now I changed it so it would format after the str_replace and it seems to work just fine.
also would appreciate if someone could explain me why^^.
Instead of explode and hard coded conversion, prefere using DateTime::createFromFormat if you have PHP 5.3 or later.
$date = DateTime::createFromFormat('d. m. Y',$fristdatum);
echo $date->format('Y-m-d');//echoes 2012-12-14
Now that you correct your script to register your dates the right way, you should ensure your database is good.
You can use this request I think :
UPDATE yourtable SET yourdate=CONCAT(MONTH(yourdate),'-',DAY(yourdate),'-',YEAR(yourdate)) WHERE MONTH(yourdate) > 12
The DATE type is used for values with a date part but no time part. MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format. The supported range is '1000-01-01' to '9999-12-31'.
The DATETIME type is used for values that contain both date and time parts. MySQL retrieves and displays DATETIME values in 'YYYY-MM-DD HH:MM:SS' format. The supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59'.
The TIMESTAMP data type is used for values that contain both date and time parts. TIMESTAMP has a range of '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC.
So if you want to store it like 14-12-2012 then use its datatype as varchar.
Convert it into Y-M-D format. You should directly put 2012-12-14 onto your database.

How can i take sql current timestamp and output as php date()? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Formating an SQL timestamp with PHP
I have a column in my SQL table that has default current timestamp values. I am trying to output it in a more readable format using date().
However, no matter which format I use, I always get the same date: 1970-01-01 00:33:32
Here is an example value of the current timestamp from the DB: 2012-08-09 06:37:58
Below is the code I use to try to "convert" it to a readable format:
$value['current_date']// is the var from the database.(containing 2012-08-09 06:37:58)
$somevar = date('Y-m-d H:i:s', $value['current_date']);
Thanks in advance.
Your problem is that you are using the wrong value in the second parameter. The date() function expects a UNIX-style timestamp as the second parameter, not a string representation of a date. Use the strtotime() function to correct this:
$somevar = date('Y-m-d H:i:s', strtotime($value['current_date']));
As someone else pointed out, however, why are you bothering to do this formatting? The format style you want is already in the database. It should be as easy as:
echo $value['current_date'];
The problem that you are having is because the second argument of the date() should be a timestamp, and since you are passing the raw string containing 2012-08-09 06:37:58, php does not know what to make of it.
Modify your code as below and it should work:
$somevar = date('Y-m-d H:i:s', strtotime($value['current_date']));
You can now use any date formats in place of 'Y-m-d H:i:s' as you wish
First of all: Why formatting the timestamp if it already exists in the desired format?
Regarding the actual problem:
See http://de2.php.net/manual/en/function.date.php
date() only accepts unix timestamps (seconds since 1970-1-1 00:00:00 UTC) or no timestamp at all. if you want to work with a timestamp like you have you need to create an unix timestamp first with date_create(), mktime , or strtotime

Convert string from datetime php [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
convert date string to mysql datetime field
I have a DateTime column in MySQL database. I would like to convert Form text field data on POST (string) to DateTime before inserting into the database. I appreciate any suggestions.
Depends entirely on what format your POST data is in. Quick/dirty/probably-will-blow-up-and-steal-your-belongings method is
$date = date('Y-m-d H:i:s', strtotime($_POST['yourfield']));
date() function.
You can find the function in php.net
It depends on format of MySQL date time field and the format of the form field. Normally db datetime is Year-Month-day Hour:min:sec for example, "2011-10-24 14:25:20". Now to convert form submitted date string to datetime, do the following:
$date = strtotime($_POST['form_date']);
$DatabaseDate = date("Y-m-d H:i:s", $date);
Now you can use the above $DatabaseDate to insert datetime into database:
#mysq_query("INSERT INTO TEST_TABLE (id, date_test) VALUES (2,
'$DatabaseDate')");
Hope it helps.
instead of strtotime($_POST['yourfield'])
use the function mktime, in that way you dont need to worry about the string format

Categories