Drupal date formatting - php

In lieu of using php's date() function directly, I'd like to trigger a date format that I've created at admin/settings/date-time/formats.
At first glance I thought I could do this:
format_date(strtotime($date), 'customformat');
But it looks like format_date() has a few formats hard-coded and doesn't communicate with the date/time formats. I could use $type='custom' but that would be just like php's date() with some timezone logic. Of course I want to define date formats in one place, and use those formats in my code.

You can select in the admin interface how short, medium and long formats for format_date should look like, but you can't define extra formats like, mycustomformat.
What makes format_date different than date, is that the result is translatable. Very handy for sites that aren't english.

Related

Human to PHP time interpreter

We've got a collection of messy data, and trying to unify it.
Lots of services let you type dates out into different formats and they correctly understand them, but cant think what the process is called, or how we could go about doing this in PHP, if there is a library that already provides this.
So we've got time and dates in an old database we've inherited, and trying to clean it up a bit, some of the formats look like
9pm
9:00pm
25th march 2015
its a complete mix and match, does anybody know of any libraries or ways to be able to parse these into a universal format?
the problem here is the information you have is inconsistent! you need to normalize it some way, IT might actually be worth getting into an excel sheet and try to match the date time fields into some kind of regex and filter like that, is probably what i would do, so you can separate the different formats and tackle each format individually.
A program will have to first identify the format you're feeding it and then it will spit out whatever format you want!
you can use this strtotime() PHP with this to turn it into any format
http://php.net/manual/en/function.date.php

Wordpress most flexible format for dates

I am using a library called Custom Metaboxes and Fields for Wordpress to add meta data to my custom post types. It's an event website.
I had it all going, but started to have issues with sorting when trying to filter the adjacent_post_links from singe posts.
I figured it might be that I am storing the date in m/d/y and not Y/m/d.
So, i've started over trying to work with a UNIX date timestamp as I have read this is the way to go. Now, I am having issues with my custom wp_queries pulling my filtered posts on the front page. I think it has to do with needing to work with timezones.
Anyways, over the long run, what are the best practices in storing date in mysql? I've also been reading about using STR_TO_Date() to properly format date so that you can order and compare dates if needed. Would this be an option if I went back to dates being stored in m/d/y .
Thanks
If you store dates as Unix timestamps in the database, it 'll gives you lots of trouble because it is not easily readable.for Example(unix format): '1410738238'
if you store as datetime format in the database, it is easy readable efficient and very flexiable,also mysql provides a lot of date functions.for Example(datetime format): '1970-01-01 00:00:01'
for more information http://dev.mysql.com/doc/refman/5.1/en/datetime.html

Force localization for os.date()

I'm building a server reporting system in Lua and C++, using the SMFL library and I've got a bit stuck on a potential issue.
The target of this script is that it creates a HTML file (with PHP within it) and uploads it to my webserver, for reporting purposes.
The name of the upload file will be '[process id][client IP][date].php', so we'll get something like '1234_199.123.45.7_31/01/2014.php', and clearly the date format needs to be in the European format.
I use the os.date("%x"), but it reports the date in the local machine's regional type, but it needs to be the dd/mm/yyyy format, regardless of the region settings of the client.
I've looked on how to do this, but I've only been able to get the time offset and not force os.date("%x") to use a specific region.
How can I forceos.date("%x") to use the dd/mm/yyyy format?
Lua's os.date accepts a format specifier, which is same as strftime. Based on strftime's docs, the format you want is obtained via
os.date("%d/%m/%Y")
You could use:
os.date("*t")
This returns a table with the date-time components, like:
With this data you can build a custom formatting function, based in T.day, T.month and T.year

RSS wrong date in PHP

I want to have the date format j/n/Y in my RSS.
e.g: 31/5/2011 display in the RSS file 05/07/2013 00:00:00.
I am using
date ( "j/n/Y" ,time() );
(I'm taking the date from DB)
How are you generating this time string. Is strtotime() involved in any stage in that process? That function, while handy in some situations, is at best "artificially stupid" and will mis-interpret a wide variety of dates as something completely different.

Auto converting data from a datetime field to a specific format in CakePHP

Hi is possible automatically to format (using date()) all data from a datetime field in CakePHP?
I'm thinking about using a callback function in the model but I don't know if I could filter fields coming from a datetime type.
Thanks in advance!
If it's something you just want to apply a single model you could use the afterFind and beforeSave callbacks to reformat the date.
If it's something you want to apply to many models you should create a Behaviour for it. You could use an array that tells it what date fields it should format.
I think I'm missing something in your question, but if you can get a date into a common string format, you can format it with PHP's date function and the strToTime function.
$date = 'January 27th, 2008'; //almost any format for dates in common usage
echo date('Y-m-d', strToTime($date));
//will print
2009-01-27
If you don't feel like hacking CakePHP's codebase, why don't you just code a wrapper function for find inside your controller? You will have to stick to a naming convention to easily identify your datetime fields but hey naming conventions are good right?
Something like: http://pastebin.com/mbbe91fe

Categories