I'm writing a program in php where a user gets credits monthly based in their registration date.
I'm writing a cron job that would be run daily.
I need to determinate if a date if a due date
In others words;
If a registered in date 2010-02-10 and today is 2011-07-10 I should get credited.
So the question is how can I deteminate reliably in php that two dates are separated by a whole month.
Or alternatively how can I get the next due date. For example today is 2011-07-18 so the next due date would be 2011-08-10
If you convert your date (fetched from the database, most likely) into a DateTime PHP object, you can use the DateTime class' methods to add exactly one month and compare that date with the current date.
As an example from the PHP documentation, this snippet adds 10 days to a given date.
<?php
$date = new DateTime('2000-01-01');
$date->add(new DateInterval('P10D'));
echo $date->format('Y-m-d') . "\n";
Related
I'm attempting to use PHP's date() function to search my database for today's date, but the result is always for tomorrows date.
This code:
$date = date("Y-m-d");
is currently giving me 2018-08-02, but it should be 2018-08-01
I've seen many questions on StackOverflow for this question:
Date function in PHP gives always date one day behind
php dates off by one day
Adding one day to a date
php date ("Y/m/d") is one day off
The solution is the same on all of them; use date_default_timezone_set(), but I am already doing that in my index.php file.
date_default_timezone_set("US/Central");
I'm using a PHP framework, the index.php file is always loaded, no matter where I am in my application, and date_default_timezone_set() is the 2nd line ran for my whole application.
How can I make date() report the correct date?
I am using a timezone based script where there is deadline based on timezones. Say for example a deadline is on 7th July at 6PM for a person in IST timezone. The deadline should be 1:30 less than 6pm in Dubai as per their timezone.
I have already calculated the difference between the two timezone difference. I am stuck at deducting that calculated time from the deadline time.
I have saved the timezones in +5:40 +4:00 -4:00 this format instead of using php default ones.
Here's what I use to add and subtract time from a date.
First of all, I get the date from the database and then convert it to a DateTime object.
$date = new DateTime($date);
I use add and a DateInterval to add time. Here's an example to add hours.
$date->add(new DateInterval("PT{$hoursToAdd}H"));
And here's and example to subtract hours intead:
$date->sub(new DateInterval("PT{$hoursToSubstract}H"));
Check this out to know how to work with DateInterval and add/subtract different times: http://php.net/manual/en/class.dateinterval.php
Can you explain a little more specifically what you want to do?
You want to subtract when data already on some var inside your script or you want the SQL query needed ?
If so maybe you can use this answer ---> How to subtract 3 hours from a datetime in MySQL?
The last answer of this post maybe is the one that fit the most what you need :
Assuming you have some timezone issue and know source and destination timezone, you could convert it like so
SELECT
DATE_FORMAT(CONVERT_TZ(x.date_entered, 'UTC', 'Europe/Berlin'), '%Y-%m-%d') AS date
FROM
x
ORDER BY
date ASC;
I am developing a web application for the gym.
if he paid the fee for the existing month the area of that month is green. and after every 30 days completion, the next month area will be red if he is not paid the fee.
so the problem is that, that how can I find that the student completes the 30 days.
Its not very clear what you want to do, but I think this is what you want to do. Its pretty simple to get the distance between two dates in days.
Finding the number of days between two dates
Store the count of days in a variable or array and just use an if-statement.
Explainations
You can use the DateTime Class in order to manage your dates.
What you could do is create two object : one for the date for today, and one for the subscription date (that you will get from a database, a file, an API, ...). You can use the constructor from the DateTime class to do so.
Next, you'll have to find the difference in days between those two dates. So you'll use the DateTime::diff() method in order to do the difference between the subscription date and today's date.
And finally, you'll have to format the difference in a more human readable format, so in days for you. You can then proceed on displaying, calculating and formating your CSS/HTML accordingly.
Source-code
<?php
$subscription = new DateTime('2016-10-12'); /* change the date accordingly */
$now = new DateTime('now'); /* you can add precision on hours and minutes as well */
$interval = $subscription->diff($now); /* interval between the subscription and today */
$daysInterval = $interval->format('%r%a'); /* %r : +/-, %a : days of interval */
if ($daysInterval > 30) {
echo 'Due date is past (' . ($daysInterval - 30) . ' days late)';
/* process here all the CSS/HTML you need */
} else {
echo 'You still have some time before due date';
/* process here all the CSS/HTML you need */
}
Online demo
Online demo.
Documentation
PHP : DateTime.
PHP : DateTime::diff.
PHP : DateInterval::format.
I am trying to get the date of the first of the month in PHP.
I found this page http://www.php.net/manual/en/datetime.formats.relative.php where it seems you can do 'first day of...' but I don't see how you specify this month.
I also found The first day of the current month in php using date_modify as DateTime object and in the second answer I see you can do
date('Y-m-01');
The latter looks simple, but I don't understand how it specifies the current month. If someone can explain this to me, and also explain the difference in the two methods, that would be a big help!
$date = new DateTime('first day of this month');
echo $date->format('Y-m-d');
Ok here is my explaination, the php date() function actually has two inputs but one is optional. The first is the returned format, the second is the time you are converting to a string formatted date. If the second input isn't set it assumes you mean now, so all of these are the same:
date('Y-m-d');
date('Y-m-d',time());
date('Y-m-d',strtotime('now'));
So when you put in date('Y-m-01') it is pulling the year and month of now but you aren't making the day dynamic, you are forcing it to 01. So in reality here is what you are doing:
$date = date('Y-m') . '-01';
And of course this is server-side so it is based off of the server's clock.
I am using PHP and mysql and using either Date or DateTime to save dates in mysql database. On site I have been displaying dates the way they are saved in database.
But now I want to show dates EVERYWHERE on site using one format:
April 17 2013
or
April 17 2013 12:20:50
I know I can use date and strtotime functions to display dates in above format. However there are a lot of places where I have date displaying code. So I am looking to automate the process where my current code works and displays dates in above format.
Any idea of how mysql trigger or some php magic could be created that converts all dates run through SELECT query automatically without changing my sql or php code since I have a lot of places in my code and it would be overkill to change code at all places?
For Example:
Date Saved in DB: 2013-04-16 12:41:26
SELECT QUERY: SELECT * FROM myTable
PHP: echo $row->dated; displays 2013-04-16 12:41:26
I want that without changing my php code, dates should be shown in above mentioned format globally on whole site.
Any ideas please how it could be achieved ?
You can directly format in via query using DATE_FORMAT()
SELECT DATE_FORMAT(myDate, '%M %d %Y %h:%i') myDate
FROM TableName
SQLFiddle Demo
and echo in your PHP: $row->myDate
MySQL Trigger doesn't project values and It is only fired during CrUD operations.
I would like to suggest you an alternative approach which i love to use.
You should use the epoch time. An epoch time is basicly the number of second that has passed since 1 January 1970
One if the benefits i love is that it is very easy to calculate
differences in time since you are just dealing with number of
seconds and not a complicated format such as sec min hrs
Another benefit is that it is very easy to store since its a
integer so you can store it in a sql db and have your php code understand it without worrying about the format and things like that.
In php, if you use the time() function, it will return the epoch time.
And if you ever want to display it in a user friendly way. you can use the following code:
$epoch = time();
$dt = new DateTime("$epoch"); // convert UNIX timestamp to PHP DateTime
echo $dt->format('Y-m-d H:i:s'); // output = 2012-08-15 00:00:00
As you can see, the format of the date is very flexible and thus easy to use.
A nice example to find the date 1 week ago:
$epoch = time() - 604800; //604800 seconds = 7 days
$dt = new DateTime("$epoch"); // convert UNIX timestamp to PHP DateTime
echo $dt->format('Y-m-d H:i:s'); // output = 2012-08-15 00:00:00