Interperet english date string to use with date_format - php

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'}

Related

Why need to convert date from string to unix timestamp by using strtotime function

I am just beginner in Laravel and as I watching certain tutorials on YouTube and author to change
format of created_at and updated_at in the view changed from string to unix timestamp and would like to ask
why we need to convert date from string to unix timestamp with the use
of strtotime function. Can't we still use string rather than
converting it to unix timestamp.
Secondly, why exactly unix timestamp over date time. Are there any
advantages of unix timestamp over date time
You do not need to use it unless you are required. You will know the reason if you ever required it.
The purpose of the function as per the PHP manual is,
The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in now, or the current time if now is not supplied.
In summary, it converts the date given in English to the number of seconds in Numeric.

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.

String to Date Format

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.

How to deal with DATE-type in MYSQL & PHP?

I'm building a website with php and i'm using the DATE-type in my MYSQL table to store dates. The problem that i have is that this stores the dates by default in the format YYYY-MM-DD. But i need this format DD-MM-YYYY to appear on my PHP page with the possibility of calculating the amount of days between 2 different dates. How can this be achieved?
That's a display problem. Use mysql's date_format() function to convert to whatever your display requirements are, e.g.
SELECT date_format(datefield, '%d-%m-%Y'), ...
You can use strtotime to convert a string representation of the date to an actual date object in php, then use the date function to spit out the date as any string format you wish. Also, you can be strtotime to perform date calculations. Additional information can be found at this blog post.
$phpDate = strtotime($stringDateFromDb);
date('d-m-y', $dateFromDb);
strtotime('-3 days', strtotime($stringDateFromDb));
Here is an example for a way to do it:
$date_str = '2012-05-20'; //you get it from db
$date_now = strtotime($date_str); //convert it to unix timestamp
$yesterday=$date_now-24*60*60; //make calculations
echo 'yesterday was: '. date('d-m-Y',$yesterday); //date() returns the date in format you need
Further example here: How to calculate the difference between two dates using PHP?

PHP convert date from a MySQL query

I think this is a simple question. We have a MySQL database with a DATE field, the date is stored in US format (2010-06-01).
In my PHP page where I'll display the date, I simply want to convert this date into a UK format (01-06-2010).
Any help and advice appreciated!
Thanks,
Homer.
You didn't specify and your example is ambiguous, but I'll assume you're talking about outputting in day-month-year format. You can use strtotime to parse a string date into a unix timestamp, and then give that timestamp to date along with the format that you'd like to output your date in:
date("d-m-Y", strtotime($date_from_mysql));
The manual page for date lists all the formatting options that you can give. In this case, d represents the day as a zero-padded number, m represents the month as a zero-padded number, and Y represents the year as a four digit number. Any characters that don't have a specific meaning for date come across as-is.
You can do it right from mysql using DATE_FORMAT() function.
example : "SELECT DATE_FORMAT(date_column,'%d-%m-%Y') as my_formated_date;" will do what you need , just make sure to use in fetch method my_formated_date column
You can parse the date with this function:
http://php.net/manual/en/function.strtotime.php
It will return an integer which is number of seconds since 1970 (called a Unix timestamp).
Then use this function to format that number any way you like:
http://ca3.php.net/manual/en/function.date.php
You can also create a Date object in PHP using the DateTime::createFromFormat feature.
<?php
$date = DateTime::createFromFormat('Y-m-d', $sql_date);
echo $date->format('d-m-Y');
?>

Categories