What is the best way in php to take the following string mm[some char]dd[some char]yyyy and translate it to yyyymmdd?
I will probably want in the future, according to local do the same with dd[some char]mm[some char]yyyy.
If there is a way that already uses the Zend Framework API, the better
<?php
$str = '08-24-1989'; // can be in any recognizable date format.
$new_str = date('Ymd', strtotime($str)); // produces "20090824".
?>
You can replace Ymd in the second statement above with any date format characters found here.
If you're looking to use Zend's Zend_Date framework, check out some examples and documentation here. Quite frankly though, the PHP functions are a lot simpler and easier to use in your case.
date('Ymd', strtotime($time));
Strtotime is absolutely the best tool to translate almost any time format into a standard one that you can then use Date to put into the format you want.
Because you question title says MySQL Dates, this is the string format that mysql uses.
date('Y-m-d h:i:s', strtotime($time));
Unless [some char] varies , use the mysql str_to_date function, e.g. STR_TO_DATE('12|23|2009','%m|%d|%Y');
I would absolutely use TIMESTAMP for any date storage. It's incredibly easy to handle time differences (like SELECT ... WHERE date BETWEEN 2138728753 AND 376251237) and can be translated to any locale pretty easily :)
Related
I have a date field that I would like to express through a MySQL Query or as a PHP function as yyyymmdd, basically without any - or /, all the examples I can find include these.
I could build it a piece at a time, so y.m.d, but thought someone here might have come across this for, its basically a naming convention for network folders, hence why no special characters.
Thank you in advance
You can use PHP's DateTime Class:
echo (new DateTime($dateField))->format('Ymd');
SQL you can use DATE_FORMAT
DATE_FORMAT(NOW(),'%Y%m%d') //20150513
PHP you can use using date
echo date('Ymd'); //20150513
You can use strtotime() and date() function like this:
$time = strtotime($yourdatefield);
$date = date('Ymd', $time);
I am trying to put a readable time and date as part of a file name (in php). I am having all kinds of trouble with this and was hoping someone could help. I have tried several different recommendations that I have read around the internet (plus I read the manual) but I really haven't gotten anything to work right. Right now I have this:
$Time=strtotime("now");
$date=DateTime::createFromFormat('m/d/Y H:i:s', '7/24/2012 14:40:30');
$date_readable=$date->$Timestamp();
At that point I then add $date_readable to a file name. It compiles and runs but it doesn't format the date at all. It still gives it as a timestamp.
Any suggestions on how to make this work?
you can do it with simple date function for example
$time = strtotime("now");
$formatDate = date('F jS, Y h:i:s A', $time);
echo $formatDate;
this will print something like
July 25th, 2012 1:02:29 am
DateTime class is more powerful then using simple date function, as DateTime class offers powerful API's plus it is object oriented. however for simple date conversions i would stick to php's date function. as that could do my purpose.
for more formatting option have a look at this link http://www.php.net/manual/en/function.date.php#refsect1-function.date-parameters
If I have a MySQL table field of my one date with this format (09-13-2011 11:22:43), as I do for PHP to print with this format (09/13/2011 11:22:43) Sorry my ignorance I searched the php.net site but can not find something about my request, Apologies.
$mysql_date = '09-13-2011 11:22:43';
$date = DateTime::createFromFormat('m-d-Y H:i:s', $mysql_date);
echo $date->format('m/d/Y H:i:s');
Use:
date( "m/d/Y h:i:s", UNIX_TIMESTAMP($fieldname));
If your field is already timestamp use the following:
date( "m/d/Y h:i:s", $fieldname);
I may be wrong in saying this but I don't think theres a standard way in doing so, unless you want to save the date/time as a unix_timestamp. If you do then you can format the time in however you want using the php date() function. If you aren't then you can always use something like str_replace() on the times to get them to the format you want or even use regex if your feeling adventurous
MySQL's DATE columns format is fairly irrelevant. Just use DATE_FORMAT() to convert the date to a string that suits your needs.
I'm currently scraping content from a website using PHP and YQL. I need to convert an awkward date format into a UNIX timestamp so it can be formatted into a MySQL compatible date. I have tried using strtotime() but to no avail. Maybe a regular expression is the answer?
Examples of dates
08Dec10
06Aug10
29Jul10
07Jun10
04May10
Dan
If you are using PHP 5.3+
Then then DateTime::createFromFormat function is perfect for your needs..
$date = DateTime::createFromFormat('dMy', '08Dec10');
Then you can do whatever you want with the date, if you need it in a different format:
echo $date->format('Y-m-d');
If you are ever able to use the DateTime functions, several available from 5.2+ and more added in 5.3 then you should. Compared to the other functional solutions, the DateTime approaches are much more readable.
Also in all solutions, whether functional or using the DateTime object, check the return values. Both createFromFormat and strptime return false on error. So you can log the error and determine what the issue was.
Note: strptime is not available on Windows.
One solution is to use strptime:
$parts = strptime($str, '%d%b%y');
and then you can pass the values to mktime:
$timestamp = mktime(0,0,0,$parts['tm_mon']+1, $parts['tm_mday'], $parts['tm_year']+1900);
Break up the date components using substr and use strtotime to put it all together again.
<?php
strtotime('20'. substr($time, 5, 2). '-'. substr($time, 2, 3). '-'. substr($time, 0, 2));
?>
can anyone suggest the neatest way to do this comparison? I need to test if a date provided in dd/mm/yyyy format is less than a fixed date, e.g 01/05/2009 - I know I can convert these to unix time format using strtotime, then make the comparison, but I'm curious if there is a quicker way to do it - many thanks, just looking for a hint to improve my code!
One option is just to reverse the format - create a string of the form
yyyy/mm/dd
That's a sortable format, so you can just do an ordinal string comparison. Of course, this means you won't detect invalid data etc.
There's probably not a shorter way code wise, and I wouldn't bother optimizing this unless you're sure it's a bottleneck.
However, as long as you're sure it will always be the exact same format (including 2 digits for month and day and with 0s) you should be able to reorder the string to put the year first, then the month, and then the day, and then just compare the strings.
However, I'm not sure this would be faster than just letting the libraries convert to unix time and then comparing those.
If you can change the format that the dates are stored in, you could define them as yyyy/mm/dd and then just compare the strings.
I think that your solution of converting it to Epoch then doing a comparison is as fast and as clean as your going to get.
if you want to improve your code, I would suggest you to use DateTime class. It's been introduced in php 5.2.0 (so, it still might not be supported on all servers)
in php 5.3.0 you can write something like this:
$d1 = DateTime::createFromFormat('d/m/Y', '02/03/2009');
$d2 = DateTime::createFromFormat('d/m/Y', '02/05/2009');
$interval = $d1->diff($d2);
echo $interval->format('%R%d days');
Well, you could use the PHP date class, but I am not sure it would be any better than you current solution...
http://php.net/manual/en/book.datetime.php
use this method
yearmonthday
you have 01.05.2010 and 03.07.2010
and to compare : 20100501 <= or => 20100703
$fixedDate = '01/05/2009';
$variableDate = '28/04/2010';
// Now we do our timestamping magic!
$fixedDate = implode('', array_reverse(explode('/', $fixedDate)));
$variableDate = implode('', array_reverse(explode('/', $variableDate)));
if ($variableDate < $fixedDate) // 20100428 < 20090501
{
// there you go
}