Comprehensive Date Comparison PHP function - php

Quick embrassing question.
I have been looking for a PHP function that would calculate the difference between two timestamps and output the result based on given parameters such as
the diff in years only, diff in months only, diff in days only, etc etc
The function I made has been quite buggy and I haven't found a good one on the Net.
Please assist.
Thanks

Please take a look at DateTime::diff()
DateTime::diff — Returns the difference between two DateTime objects
You can format the output to anything you want it to be
(Provided an extra answer despite of the duplicates because they use strtotime & math and that doesn't always work out well or is a nice way to do it. Using a core function of php seems nicer to me)

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

Convert date to string in php to zulu UTC format, not a duplicate topic?

Hi I have posted this question before, but people marked it as a duplicate questions. so I didn't get any answer.
I am trying to get the current date/time in the format for example "20150701183741.941Z', I am not sure what the technical term for this format is called. some people told me that it is UTC/ZULU time format.
I have tried generate the above format by using below codes.
date_default_timezone_set("UTC");
echo "formatted currenttime: ".date("YmdHis.ue", time());
but it returns it in the format "20150701144710.000000UTC", this is close to what I need, but I am not able to get at the end something similar to ".941Z".
Please note that this isn't a duplicate question. I posted it earlier but got marked as a duplicate and that one is now inactive. Thanks very much for your help.
The official documentation of date() says that date() always generates 000000 because it takes an integer parameter, and that DateTime() does support microseconds:
http://php.net/manual/en/function.date.php
However, this comment seems to say that DateTime() doesn't support microseconds either:
http://php.net/manual/en/class.datetime.php#108970
So I guess you're out of luck with that :(
Why don't you use the Carbon Library? It's one of the superb ways to handle data and time. Just take a look into the official documentation at http://carbon.nesbot.com/docs/

How to calculate the number of days between two dates starting from B.C.5000?

Would like to have this function but with before christ date and taking account into gregorian and julian calendar. I have found solutions for a.d. dates but have not found one for b.c. and my solution requires a lot of manual calculation. Is there an elegant way to do so in php I have missed? An existing built in function I mean.
http://www.timeanddate.com/date/duration.html
I rewrote the original calendar.php function and add in my own solution.
Thanks.

How can I compare a date (day/month) in PHP [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
How to compare the date parts of two Zend_Date objects?
I've been trying to compare dates, but for some reason things don't seem to work as I want them to.
I'm trying to log to a database whenever someone logs into my site on a certain day.
I've used if (date("j F") == "25 July") followed by my code, but it doesn't seem to trigger.
I intend to get it to check for a lot of things, such as Friday 13, 17 March, etc (obviously I know I'll need to change the date() format for Friday 13).
The current check of 25 July is a copy from an echo of date(), so I know it's definitely not a typo. Can anyone tell me why this isn't working, and how to do it?
Cheers
The best method is to use a strtotime function. But I think it won't work until you specify year in the date.
Not sure about it though. But strtotime is a good option.

What does wikipedia use for text and revision diffs

I'm trying to take 2 versions of text (10 pages long) and compare the 2 to produce the difference. I know Wikipedia has a similar feature to compare revisions. Does anyone know what they use? I'm hoping they're using a php-driven solution.
There is an implimentation of diff in php. I haven't used it but it's a start. There is also something called PHP inline diff that you can check out

Categories