PHP Relative Time - php

Hello I have a question about relative time formats in PHP. I am looking to get the time "month to date." For example, Today is May 5th, I would like to get the time span from May 1st, to May 5th. I tried using the format "this month," but I did not have success. Can anyone point me in the right direction?

Will strtotime() work for you? It takes a string such as next month and then creates a timestamp of it.
If not, take a look at this Stack Overflow question.
Another option would be using DateTime which is builtin to PHP as well.

Related

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 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.

SO dates calculation and formatting

What script SO uses for dates displaying? Because it seems to be pretty nice formatting and logical showing.
I am not sure what StackOverflow use. But one of the most common timestamp representaion is the one done by twitter which displays timestamp as a moment ago, 30 seconds ago, x minutes ago, yesterday, 10:30 PM Apr 12, 2010 etc. And it updates the timestamp every five seconds without hitting the server.
If you are interested you may look into John Resig's Pretty Date JS API. It's just awesome. Works with/without JQuery.
We tweaked it a bit to exactly match Twitter pattern. And it is awesome.
The website mentioned gives good example, but if you so want a working version, put the following script in address bar of any web-page. (you may want to tweak parameters passed to prettyDate function
javascript:var i,s,ss=['http://ejohn.org/files/pretty.js'];for(i=0;i!=ss.length;i++){s=document.createElement('script');s.src=ss[i];document.body.appendChild(s);}alert("PrettyDate: "+(prettyDate("2011-03-13T03:24:17Z")?prettyDate("2011-03-13T03:24:17Z"):"03:24 AM Mar 13, 2011"));
I think it's based on the answers to this question on Stack Overflow asked by Jeff Atwood - most of the answers are in c# but there is a PHP implementation too

display time in a web page

I have page where where I list all comments for a post. Next to each comment I have a time value in full format including the date/time (2010-01-02 11:11:20).
I know that I can format it in PHP before displaying it, so it shows;
posted 40 secs ago
posted 5 days ago
but that would not be efficient as I am going to cache the page once it is generated.
On SO I see that they have some kind of java script for showing it, so it is run on the browser of the client:
alt text http://img528.imageshack.us/img528/5442/35118769.png
So basically I need a java script that runs on the browser that would instead of showing my date/time show "posted 4 hours ago". All help is welcome.
Sounds you want this: timeago: a jQuery plugin.
Timeago is a jQuery plugin that makes it easy to support automatically updating fuzzy timestamps (e.g. "4 minutes ago" or "about 1 day ago").
I quote further:
Avoid timestamps dated "1 minute ago" even though the page was opened 10 minutes ago; timeago refreshes automatically.
You can take full advantage of page caching in your web applications, because the timestamps aren't calculated on the server.
So it should fit your needs.
You can put the "timestamp" of the page into the page when you render it. This is the fixed time that doesn't change. Look at PHP's time function for this (http://www.php.net/manual/en/function.time.php). It gives you seconds since the UNIX epoch started.
Then, when the page is running, use Javascript's Date object. It encapsulates something similar. Do a getTime() on a new Javascript Date object, which will get you also seconds since the UNIX epoch.
Subtract one from the other to get the elapsed time between then and now, and do whatever pretty formatting you need to make it display right in your page.
Edit: See also Felix's answer about timeago, which is a nifty tool to do the second half of this process for you automatically.
Output your date in a format accepted by the parseDate method or as a constructor parameter for date. Place them in some nodes which can later on easily be grabbed by a javascript framework (or your own methods) and then perform some time/datediff methods on these grabbed values and replace the node's content.

How to create a friendly date format (for example "submitted 2 days ago") [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How do I calculate relative time?
I want to format dates on my social web app much like Digg.com and other sites do. There, you see very friendly dates, such as:
just now
3 minutes ago
one hour ago
2 weeks ago
6 months ago
etc
Before I wrap my head around creating such a thing, does anyone know of any ready-to-go script for this where I simply insert a datestamp and a friendly date text is given based on how it related to the current time?
PS: I need this in PHP, but pseudo-code or any other language is fine too.
This is a duplicate of this question. It has a flurry of code samples on how to accomplish this, in addition to the code this very site uses. I glanced at it and there seems to be a PHP implementation posted there too.
In addition to all this, if are you using jQuery you can do this client-side with something like the timeago plugin. It has the advantage of updating the text as time passes so if you load a page and it says "posted 5 minutes ago" and look again 5 minutes later, it says "posted 10 minutes ago"
Thanks all for the answers, and sorry for the duplicate question. I did not find the duplicate when I was looking for it because I did not really know what search terms to use.
Anyways, I have my problem solved thanks to the PHP translation of the code used by stackoverflow. I made one tiny change in calculating the delta:
$delta = strtotime(gmdate("Y-m-d H:i:s", time())) - $time;
Since I am storing my dates in MySQL as timestamp in the GMT format, I have to use the same for calculating the CURRENT time. This makes for a timezone neutral comparison, which is exactly what is needed in my case.
You can also do this in SQL:
Best way to convert DateTime to "n Hours Ago" in SQL

Categories