String to Date Format - php

PHP Mysql i want to convert date format from mysql... please help me.
Like
SELECT * FROM agentcommission where AgentId='AC-1363717254' AND Date between STR_TO_DATE('24-Mar-2013','%d-%M-%Y') and R_TOST_DATE('31-Apr-2013','%d-%M-%Y');
but it shows Zero rows.
Like
18-Apr-2013 is my date format and i want to convert it to 18-04-2013 Means string to date format

you can use DATE_FORMAT function to convert date from one form to another.
select DATE_FORMAT(STR_TO_DATE('24-Mar-2013','%d-%M-%Y'),'%d-%m-%Y');
This statement will give you the required formatted date as 24-03-2013.
But as said above you need a YYYY-MM-DD format to use the BETWEEN operator to compare dates in your custom date format.

You need %b not %M
%b Abbreviated month name (Jan..Dec)
SELECT STR_TO_DATE('24-Mar-2013','%d-%b-%Y')
Returns
March, 24 2013 00:00:00+0000
If your date column is a date type then you need a YYYY-MM-DD format. You can't use the BETWEEN operator to compare dates in your custom date format.

Related

How to convert two different strings to date and store it in single column in MySQL database?

Currently I'm working on PHP and MySQL Project. I'm taking DATE INPUT from users in string format. So user can enter any format in text-box. For e.g. dd/mm/yyyy or dd-mm-yyyy or yyyy-mm-dd or mm/dd/yyyy etc. (I know its too bad practice). But my question is:
is it possible to convert these all in single format and store it in
single column?
.
For converting I'm using str_to_date() function, but it accepts only one format to convert. How can I add other formats to convert string to date ?
try this, should work:
$mysqlFormatedDate = date('Y-m-d H:i:s', strtotime($yourTime));
PHP will take $yourTime and resolve it's format, then it will convert it to unix timestamp, and then it will convert to mysql datetime format which is Y-m-d H:i:s then you just need to save it to your DB.
you can do this in your mysql_query using STR_TO_DATE:
STR_TO_DATE($yourTime, '%c/%e/%Y %r')
You can use "RLIKE()" to check fo some patterns but that will fail because you can't differentiate between international and retarded. Imagine someone typing in "12/11/2018". Is it the 11th of December or 12th of November? Also you wrote "e.g.", so if you allow everything, what would you make of "20122007"? 20th December 2007, 20th July 2012?
Use a JS-Library for date inputs or do inputs for day, month and year separately. Otherwise it's pretty hopeless.

Interperet english date string to use with date_format

In smarty, I have a date string formatted like this: DD/MM/YYYY
When I try to use date_format it gets the date wrong.
How can I make it understand what the initial string is formatted like?
The documentation of date_format Smarty variable modifier explains what it expects for the value to be formatted:
This formats a date and time into the given strftime() format. Dates can be passed to Smarty as unix timestamps, DateTime objects, mysql timestamps or any string made up of month day year, parsable by php's strtotime().
The date format you are using (DD/MM/YYYY) is not recognized by strtotime() and there is a good reason for it: for more that one third of the days of the year, this format cannot be told apart from MM/DD/YYYY. The PHP developers had to choose which of these two formats to recognize and they have chosen MM/DD/YYYY (probably because it has higher coverage).
A possible solution to your problem is to use the replace modifier first, to change / to - or .. Both DD-MM-YYYY and DD.MM.YYYY formats are valid and recognized by strtotime() and they do not clash with other formats.
If your current code looks like:
{$date|date_format:'%e %B %Y'}
change it to:
{$date|replace:'/':'.'|date_format:'%e %B %Y'}

get month & year from Mysql Date format m/d/y & compare with today's month & year

I have mysql date format m/d/y as varchar like 03/12/2015 for 12 March 2015. Now I want to count all the result from mysql table in which the month & year are the same as of today's date.
I am using following query, but no sucess
SELECT * FROM student where DATE_FORMAT(regd_date,'%m/%y')=DATE_FORMAT(now(),'%m/%y')"
Thanks for any help
You will first need to use STR_TO_DATE() to convert your varchar column to a date before using DATE_FORMAT().
SELECT * FROM student
WHERE DATE_FORMAT(STR_TO_DATE(regd_date, '%m/%d/%Y'),'%m/%y') = DATE_FORMAT(now(),'%m/%y')
Adjust the format for STR_TO_DATE() accordingly.
Note: I would discourage storing dates as varchars.

PHP MySQL date functions

I am working with dates, in both PHP as well as MySQL. EVerytime I use to convert date in unix format. But this time I have taken field in DB as date. But issue is it is taking yyyy-mm-dd format. I want to store it in dd-mm-yyyy format. Is this possible if I set default setting of DB. or each time I have to explode the dd-mm-yyyy format in PHP and convert it in YYYY-MM-DD format. Its my first query.
Second query is I wish to fetch the records from today's date. I mean dates after today's date. Like today then tomorrow then so on.... Is it possible to use order by on date field.
Just use:
$date = date('d-m-Y', strtotime($dateFromDB));
That will convert from MySQL DateTime to the format you have specified.
It is possible to order by date fields, e.g.:
SELECT *
FROM table
WHERE date > [yourDate]
ORDER BY date [DESC | ASC]
Your second requirement contradicts with the first one.
If you store your date in dd-mm-yyyy format, you'll be unable to sort your dates.
So - yes, you have to "explode" the dd-mm-yyyy date in PHP or format it any other way. That's not a big deal though. Everyone does it.
If you have a field of type 'datetime' you can use the MySQL-Command: FROM_UNIXTIME(%d) for conversion. 'order by' should be no problem.
Store the date in default format that is yyyy-mm-dd
when you want to display in front end
use the following query to
select otherFields, date_format(dateField,'%d-%m-%Y') from tableName;
For ordering by date
SELECT * FROM tbl
ORDER BY date DESC

How to format a date field in mysql?

I am having a problem in mysql query, I have date saved in database in this format :
Wed, 26 Oct 2011 01:25:35 EDT
I want to sort the rows by date but this date format is not letting me do it. I have tried Date_FORMAT AND STR_TO_DATE function but couldn't get it working, could you please help me solve this?
Assuming you've modified your tables so you've got the original date field ('olddate') and want to put the reformatted 'native' date into a new field ('newdate'), then:
UPDATE yourtable
SET newdate = STR_TO_DATE(olddate, '%a, %e %b %Y %H:%i:%s')
however, MySQL does not store timezone information in its date/time fields, so you'd have to convert the EDT stuff to whatever TZ is desired (UTC?).
Once converted to native format, you'd do your sorting with:
SELECT ...
...
ORDER BY YEAR(newdate)
This format is just plain text to MySQL and you should have another one as datetime or timestamp with the same data but something that MySQL could understand.
add a new column
use PHP to strtotime your actual row and replicate that value to the new column.
…
profit!

Categories