Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I'm trying to get the day after the easter date:
date("d-m-y", strtotime(easter_date(), '+1 day'));
I don't understand, the date showed is still the same, but when I try this:
date("d-m-y", strtotime('+1 day'));
It works.
How can I do?
That's because easter_date() produces a unix timestamp:
1364713200
And strtotime() is meant for this like 'today' and 'tuesday next week';
If you just want to add a day to it, just add 86400 (this is how many seconds are in a day) to easter_date();
So: date("d-m-y", easter_date() + 86400);
The function easter_date() returns a UNIX timestamp (e.g. 123338898) whereas strtotime()'s first parameter must be a valid date/time string (e.g. 2013-05-30 23:59:59). Valid formats are explained in Date and Time Formats.
Additionally, from the PHP documentation for strtotime():
Using this function for mathematical operations is not advisable. It
is better to use DateTime::add() and DateTime::sub() in PHP 5.3 and
later, or DateTime::modify() in PHP 5.2.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I am using the php function date() to take a timestamp and turn it into a readable date. It is stored under type=timestamp, function=now in mysql, and the timestamp I'm using reads "2013-12-17 16:23:00" in the database. And it can echo out perfectly using $date1. However, when I try to convert it using this code:
echo date('m/d/Y, $date1)
It turns into "12/31/1969" on the page. I have no idea why.
To convert a MySQL timestamp you'll need to make use of the strtotime function
date('m/d/Y', strtotime($date1));
The function strtotime will take must formats of date/timestamp and convert it into a unix timestamp which can then be converted to another readable format using date(). Reference: http://php.net/strtotime
The above works, but it's not to say that it's the best way of doing so, I'd be happy to hear alternatives.
The date you're getting is because date() expects a unix timestamp (seconds that have passed since 1/1/1970), and I assume that your web server is configured to a UTC-, so you're getting the day before. It's an annoying bug that can come from the way that MySQL handles timestamps.
you must use the strtotime function in order to get the correct date format
print date( 'm/d/Y' , strtotime( $date1 ) )
Hope it helps your need
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
When using PHP to do time() and store it in a MySQL server and then retrieve it,
when I try to convert it into seconds, it returns strange results.
1386787112 = 35 seconds ago (What my PHP says)
1386787112 was actually 1 second ago (What my current time says).
$da = date("s",1386787112);
This code is meant to returns the current time in second but it only updates if the time is bigger than 60 seconds. Am I doing something wrong?
Using the time() function will do what you're looking for. It's in fact defined just like your requirements.
Now, if you wanted seconds ago you could then use the time() function to do that:
$secs = time() - dbTimeValOfRecord;
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm having an application in PHP which I want to foresight how it works in certain situations which I need to rewind the time to the future.
Is there any ways to do this?
I could only find timezone function which let me change the time zone but what I want us to change it to the future like... 3weeks or something like that.
There are a bunch of ways to do this with PHP. Here's one:
$future = date("Y-m-d", strtotime("+3 weeks"));
Here's another:
$future = (new DateTime())->modify("+3 weeks")->format("Y-m-d");
Here's another:
$future = (new DateTime())->add(new DateInterval("P21D"))->format("Y-m-d");
Sounds like a job for fluxcapacitor if you're running on Linux
You can do it with
date('l jS F (Y-m-d)', strtotime('3 week'));
or
date('l jS F (Y-m-d)', strtotime('21 days')); // for future 3 weeks
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Given today's date, is there an elegant way to get the previous Sunday's and the next Saturday's date?
So, given today's date of 12/12/2013, I would want to get back 12/8/2013 for the Sunday and 12/14/2013 for the Saturday.
If given a Sunday, for example 12/15/2013, I would want to get back 12/15/2013 for the Sunday and 12/21/2013 for the Saturday.
If given a Saturday, for example 12/21/2013, I would want to get back 12/15/2013 for the Sunday and 12/21/2013 for the Saturday.
And so on.
Any ideas?
DateTime() accepts relative formats:
echo (new DateTime('last Saturday'))->format("Y-m-d");
echo (new DateTime('next Saturday'))->format("Y-m-d");
See it in action
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
There appears to be a lot of info converting a time period into a time, but not the other way around.
An example of what I need to do, is convert say 120 minutes into P0DT2H0M0S.
And 13:10 into P0DT13H10M0S.
And 120 minutes into PT2H0M0S.
Any quick and easy way to do this?
I believe the format you're describing is the ISO 8601 date/time format. Here's how it describes intervals.
In the PHP documentation for the DateInterval class, someone has shared an example of how you might convert a string into ISO 8601 in an object-oriented way:
http://www.php.net/manual/en/class.dateinterval.php#113415
And here is someone else's solution, using functional rather than object-oriented date methods:
https://stackoverflow.com/a/13301472/2119660
The easiest way, to get ISO-8601 time interval duration, is with method createFromDateString
Use:
echo getISO8601duration_withZeros("3 year 20 day 15 minute"); # P3Y0M20DT0H15M0S
echo getISO8601duration_withZeros("1 hour 120 minutes"); # P0Y0M0DT1H120M0S
echo getISO8601duration_withZeros("7 year 5 month 3 day"); # P7Y5M3DT0H0M0S
# 13:10 example
$dt = new DateTime('13:10');
$interval_spec = "{$dt->format('H')} hour {$dt->format('i')} minute";
echo getISO8601duration_withZeros($interval_spec); # P0Y0M0DT13H10M0S
Function:
function getISO8601duration_withZeros($interval_spec) {
$interval = DateInterval::createFromDateString($interval_spec);
return $interval->format('P%yY%mM%dDT%hH%iM%sS');
}
Demonstration:
examples with zeros (code above);
examples without zeros;
examples with zeros, with recalculated carry over points;
examples without zeros, with recalculated carry over points.