Same function returning different outputs - php

Well,this seems strange. Please bear with me. Someone asked this question in SO. He wants the date of previous Monday. So i suggested
$monday=date(Y-m-d,strtotime('Monday this week'))
The output was perfect in my localhost. It showed 2012-07-30. Another guy commented that the function i mentioned isn't working. It is giving the same date like
$monday=date(Y-m-d,strtotime('Monday'))
i.e., 2012-08-06. And he isn't lying! The online editor which he linked is showing next monday's date. Check this! Why is this happening??
I searched, but couldn't get the reason behind it. Is it because of the older versions of php? Any help will be greatly appreciated. Thank you

Somewhere between 5.2.17 and 5.3.10 the problem was fixed: http://viper-7.com/1PPz5m (look at the paste history).
Digging around in the changelog for the 5.3.0 release I found this:
proper support for "this week", "previous week"/"last week" and "next week" phrases so that they actually mean the week and not a seven day period around the current day.
Sounds like that's probably the answer to me. Basically before 5.3.0 this week etc may give you the wrong answer, because it will look for the day in the 7 days surrounding the current date that is a Monday, whereas in 5.3.0 and later it will be interpreted correctly.

Related

wordpress acf date trouble

Hello!
I set an ACF called pst_date and filled it with '03.05.2015' on a post.
In my template I'm using:
<?php echo get_field('pst_date'); ?>
Output shows 02.05.2015.
Same happens for other years even 2020.
Now I was curious and tried other dates and both input '29.03.2015' and '30.03.2015' have same output of '29.03.2015'.
Before the date '29.03.2015' it shows the correct date.
After '29.03.2015' it is one day short.
Same happens for other dates passed the last sunday of march.
So I assume its some DST bug.
Is there any workaround / fix?
Google was not helpful with that particular problem.
wordpress 5.3.2
php 7.0 / 7.1 / 7.2 / 7.3
Thank you.

PHP Relative Time

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.

PHP strtotime not recognizing "2012-W11" on new server

Sorry if this seems like a stupid question. I'm probably missing something obvious but I've been struggling with this for ages and am getting nowhere. I've tried searching but haven't been able to find anything specific enough so I'm asking here in the hope that someone smarter than me can help me out.
I'm using the following line of PHP code to get the timestamp of the beggining of the current week.
strtotime(date('o-\\WW', time()));
This works fine on my local server and it worked fine on my live shared hosting, but now that I have moved my site to a virtual dedicated server it returns a blank.
The date() part is working fine on all servers ie.
date('o-\\WW', time()) today returns 2012-W11 on all servers
Therefore the problem is with the way strtotime turns 2012-W11 into a timestamp ie a problem with running strtotime('2012-W11')
I'm guessing there must be a difference in the way the new virtual dedicated server is set up that is causing it not to be able to do this in the expected way but I can't for the life of me work out what it is.
Thanks in advance for your help
try changing the format to strtotime('2012 +11 weeks')
you can use below code get first day of the current week
$weekFirstDay = strtotime('Monday this week');
it's from PHP Manaul

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

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