There is a table column with the date format varchar and 0000-00-00 00:00:00.
Now I would like to display the date as a string. At the moment there is the date and even the time. I have no clue how to solve this.
I already tried converting by using strtotime():
$doa = $data['date']
...
$time = strtotime( $doa );
$date_format = date( 'd.m.y', $time );
what displays a wrong date 01.01.70.
Thanks in advance.
UPDATE:
...and here is how it will be stored in the db:
$timezone = 'Europe/Berlin';
date_default_timezone_set($timezone);
$timestamp = time();
$db_date = date("d.m.Y - H:i:ss", $timestamp);
When you're saving your date to the database, you're saving it as "varchar", so it's a string. Most probably your format is created like this before inserting the code into the database:
$date_format = date('Y-m-d H-i-s', $time);
This will convert a date to your "0000-00-00 00:00:00" format. I don't know about "months" and "days" at your format, so you might switch "m-d" pair in the above date() arguments.
Then you're pulling the data from the database, and in fact your have the correct code. You pull data in "0000-00-00 00:00:00" format, then convert it to Unix time (strtotime) and then format it using date() function. You just need to keep in mind that Unix time started at the 1th of January, 1970. This is why you're getting "01.01.70" from your code when applying zeros as a date string.
A short conclusion to all this: if you have any actual date (not all zeros) stored in your database, then all should be really fine.
Honestly speaking, PHP Date documentation has all clear details on date formatting, you can easily grab the idea.
Of course i can be mistaken and you have some more details, i just can't yet post to comments, a newbie here.
Related
I have a column in my database with date schema.
It accepts string in the form of 2018-02-21
I have a date value in this form 1/27/18, 4:15 AM
I need to format the date properly to insert it into the database.
I was trying to use Carbon Package to format the date, but no luck with it.
$date = "1/27/18, 4:15 AM";
$date->format(Y-m-d);
Obviously, this didn't work.
Could someone please help me solve this problem.
You need to convert your date to timestamp.
$date = "1/27/18, 4:15 AM";
$formated_date = Carbon::createFromTimestamp(strtotime($date))->format('Y-m-d');
It should works.
If you don't want to use carbon, you can try this with default php date function
date("Y-m-d H:i:s", strtotime($date));
Both case output will be
2018-01-27
I want to show data that from between date that I choose.
Already tried SELECT * from myTable WHERE myDATE BETWEEN LIKE 'datefrom%' AND LIKE 'dateend%'
and for CONVERT I still don't get it because haven't found one that have a full query about that.
As my title, my sql field is datetime type and with format 'yyyy-mm-dd hh:mm:ss'
while in php I have format dd/mm/yyyy
Please answer this only in PHP code and SQL query and in full mode.
Thank you :)
You can do between YYYY-MM-DD and YYYY-MM-DD (excluding time) in MySQL but MySQL won't understand dd/mm/yyyy so you need to convert it to the proper format.
When you exclude the time, it interprets it at 00:00:00 so depending on your situation, you may want to do between YYYY-MM-DD and YYYY-MM-DD 23:59:59
Note: PHP won't properly interpret dd/mm/yyyy either. PHP date functions understand mm/dd/yyyy and dd-mm-yyyy.
As commenters have said, you will first have to convert your dd/mm/yyyy formatted date to something that MySQL prefers, which is YYYY-mm-dd H:i:s
So, how do you convert your date to MySQL friendly date? Do something like this:
<?php
$datefrom = '25/06/2015';
$dateend = '20/10/2015';
// create an array with dd, mm and yyyy
$splitFrom = explode('/', $datefrom);
$splitTo = explode('/', $dateend);
// mktime takes hour, minutes, seconds, month, day, year arguments
// create timestamp using mktime and pass it to date and format it to
// mysql friendly timestamp
$mysqlFrom = date('Y-m-d H:i:s', mktime(0, 0, 0, $splitFrom[1], $splitFrom[0], $splitFrom[2]));
$mysqlTo = date('Y-m-d H:i:s', mktime(0, 0, 0, $splitTo[1], $splitTo[0], $splitFrom[2]));
// echo sprintf("From %s and To %s", $mysqlFrom, $mysqlTo);
// this is where you would do your SQL statement and substitute $mysqlFrom and $mysqlTo dates
?>
Note that this is just a rudimentary example to show you how you can proceed with solving the problem yourself. As time passes, you will have to optimize this kind of code further.
Because using timestamp leads to a better performance in mysql I want to save the time as timestamp in my database. I receive a datetime string like this: 2014-09-24 17:18:27, convert it to timestamp with strtotime($the_date_from_above) and just for fun I converted it back with date("Y-m-d H:m:s", strtotime($the_date_from_above))
I var_dumped the process:
string(19) "2014-09-24 17:18:27"
int(1411579107)
string(19) "2014-09-24 17:09:27"
What went wrong during the time string convert? What is causing the difference? It's the same amount of seconds but 9 minutes difference is strange.
You'll need to convert it back like this:
date("Y-m-d H:i:s", strtotime($the_date_from_above))
When you convert it your way the m in H:m:s gets treated as the month.
Even nicer would be to use the datetime class:
$date = new DateTime();
$date = $date->setTimestamp($yourtimestamp);
echo $date->format("Y-m-d H:i:s");
date("Y-m-d H:m:s", strtotime($the_date_from_above))
^-----^---
you use m twice, which is the MONTHS value. Minutes are actually i.
You simply use wrong date-time template "Y-m-d H:m:s" should be "Y-m-d H:i:s". Little mistake.
But what I don't understand is your reason to do it this way. Because MySql has correct column type for saving Date-Time values and type timestamp is usually use for another purpose.
For better performance and precision, if you want to use value for next calculation, can sense save in database this value as string. (means timestamp with miliseconds)
I have dates formatted as d/m/y. How can I insert them into a DATETIME column?
Use MySQL's STR_TO_DATE() function:
INSERT INTO my_table VALUES (STR_TO_DATE('26/5/12', '%e/%c/%y'))
You need to use php's date() function along with strtotime() to convert date to any format you want.
MySQL database stores the date in YY-MM-DD format for datetime datatype, so if for example you have a date
$date = '26/05/2012';
You can convert it by using date() and strtotime()
$formatDate = date('Y-m-d', strtotime('26/05/2012'));
This will convert the date from 26/05/2012 to 2012-05-26 which then can be inserted into the database.
If you are using a timestamp datatype to store the date in your database, then all you need is to convert the current date into unix timestamp and store in database for example.
$date = strtotime('26/05/2012');
//this will convert the date to unix timestamp
Update:
as pointed out by #wallyk (thank you), strtotime() does not handles dd/mm/yy format. the fix is to replace the slash / by -m below code should work for you.
date('Y-m-d', strtotime(str_replace('/', '-', '26/05/2012')));
Try this:
$mysqldate = date("m/d/y g:i A", $datetime);
$date = date('d/m/Y');
$date = strtotime($date); //in unix time stamp format
Basically american date format is MM/DD/YYYY and you are providing DD/MM/YYYY so thats why startotime() returns you a null values on this input; and i prefer you must follow standard date format of american (MM/DD/YYYY) because if you are using mentioned format of date that will create more problems as well in different places ..
if you check by this
echo date('Y-m-d', strtotime('05/26/2012') );
and it is working fine ..
you could change your DATE column into a String Column and insert the data when ever you want 2 check if the date is right you can use a regular expression to do so
I have been looking online for this answer and have come up empty...I am extremely tired so I thought I would give this a go....
I have a variable that has a date from a textbox
$effectiveDate=$_REQUEST['effectiveDate'];
What I am trying to do is take this date and add the current time
date('Y-m-d H:i:s', strtotime($effectiveDate))
When I echo this out I get 1969-12-31 19:00:00
Is this possible? Can someone point me in the right direction?
I found a solution to my problem....
$currentDate = date("Y-m-d");
$currentTime = date("H:i:s");
$currentDate = date("Y-m-d H:i:s", strtotime($currentDate . $currentTime));
echo $currentDate;
This takes a date from variable in one format and takes the date from another variable in another format and puts them together :)
Thanks everyone for their time.....
DateTime::createFromFormat
would also work but only if you have PHP 5.3 or higher...(I think)
The effectiveDate string is not in a format that strtotime recognizes, so strtotime returns false which is interpreted as 0 which causes the date to be displayed as January 1, 1970 at 00:00:00, minus your time zone offset.
The result you see is caused by the entered date not being in a format recognised by strtotime. The most likely case I can think of without knowing the format you used is that you used the US order of putting the month and day the wrong way around - this confuses strtotime, because if it accepts both then it can't distinguish February 3rd and March 2nd, so it has to reject US-formatted dates.
The most reliable format for strtotime is YYYY-MM-DD HH:ii:ss, as it is unambigous.
The date is just a timestamp, it is not object-oriented and i don't like it.
You can use the DateTime object.
The object-oriented best way is:
$effectiveDate=$_REQUEST['effectiveDate'];
// here you must pass the original format to pass your original string to a DateTimeObject
$dateTimeObject = DateTime::createFromFormat('Y-m-d H:i:s', $effectiveDate);
// here you must pass the desired format
echo $dateTimeObject->format('Y-m-d H:i:s');