Wordpress most flexible format for dates - php

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

Related

Format Carbon::Now issues on Seeder

I'm inserting some test data into my db for a posts table.
I am trying to format the Carbon post created date to read as for example if the post was created on 20-06-2016 it would read 20th June, 2016.
Here is my code
'post_created_date' => Carbon::now()->format('d-m-y')
How do I format it to display this? Currently everything I try doesn't work and it just defaults to Y-m-d
Thanks so much for any help.
Please don't get the wrong idea if you think Im being lazy, I've read the data mutator docs on the laravel website but I'm struggling to see a solution.
For a data format such as 20th June, 2016.
'post_created_date' => Carbon::now()->format('mS F, Y')
Source the php date format.
Though, I don't recommend you save dates in your database in this format. It's better to save the date in regular mysql format and do the formatting in the view.

AngularJs DatePicker and Ajax - which format to store in MySql database?

I want to create a new object, with a start & end date using the standard UI-bootstrap DatePicker.
I will send these date, using Ajax, to my server, where some PHP which I code will store it in a MySql database.
Coming from a PHP background, my first instinct is to store date/time as a Unix timestamp.
Since (Angular) JS has a millisecond resolution and Unix timestamps have a second resolution, that involves multiplying/dividing by 1000.
My server is returning data as a JSON, and I would rather not change that.
Ideally, I would like to do any conversion on the server, since it ought to have more processing power, and just initialze by AngularJS model fronm the JSON received.
Would it be acceptable to store the date as strings in the MySql database, rather than a timestamp? That way the downloaded data could be used unchanged to initialize the DatePicker.
Since I am having trouble getting the DatePicker to show the date which it receives from the server (as Unix timestamp), this seems like the simpler approach, but is there anything to watch out for, if I do so?
How do others do this?

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

Working with unix timestamps and timezones

Timezones and timestamps confuses me so I'm hoping someone can answer my questions :)
Lets say I have a Python script that parses an RSS feed, converts the date value into a timestamp using the following code and stores it in a database:
article_date = parse(article.published).strftime('%s') if hasattr(article, 'published') else round(time.time())
Now when I retrieve that record from the db in PHP, and I run the following code, does PHP assume the timestamp was UTC-0 and automatically offsets the timezone to Eastern time?
date_default_timezone_set('America/New_York');
echo date('Y-m-d H:i:s',$timestamp);
I'm seeing weird issues with my dates, so I'm wondering if someone can help me out with advice on how to properly convert and store rss feed timestamps. I can across this line of code somewhere so should I put this at the top of my script?
os.environ['TZ'] = 'Europe/London'
If you want to set your timezones and keep them aligned in PHP and in Python, then your PHP code is completely correct and for python you need to apply the following:
os.environ['TZ'] = 'America/New_York'
time.tzset()
before you call strftime()
That should make sure you store the time in the same zone you're trying to retrieve it.
Note: tzset() is a Unix-only function.

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.

Categories