Add 30 days for date in db - php

I have publish up date and publish down date in my DB.
Currently they are both same dates.
How do I change it (during mysql insert) so publish down date is 30 days past publish up date.
I am using $pubDate
Thanks

You can use DATE_ADD():
DATE_ADD(my_date, INTERVAL 30 DAY)

in php, before inserting you can use strtotime():
if the publishDown date is a timestamp:
$publishDown = strtotime("+30 days",$publishDown);
otherwise you may have to use mktime to get it in the right format

Related

How can I change my date to this format?

I currently have a date formated like this:
2017-11-02 11:44:24
However; I need it in this format: 2014-03-11T14:49:52
This is due to the use of a RESTful API based on oData. How can I achieve this date format?
This looks like a job for: DateTime:createFromFormat
$date = DateTime::createFromFormat('Y-m-d H:i:s', '2017-11-02 11:44:24');
echo $date->format('Y-m-d\TH:i:s');
Once you've got the DateTime object then you can format it as needed with ->format()
The date and time can be tricky, but what you want to be sure of is the date and time for your region or Greenwich Mean Time (GMT) or Universal Time Coordinate (UTC). Both GMT and UTC refer to the same time, but your server/database/publish date is likely in your local time. Here's an example of what you can use for local time:
date_default_timezone_set("America/New_York");
$date = date('Y-m-d\TG:i:s');
echo $date;
This will echo 2017-11-02T01:02:58. The capital G represents a 24 hour time stamp, and a small g will represent a 12 hour time stamp.
Depending on how your server is setup, without the date_default_timezone_set you will get the UTC time.

How to Substract time from datetime

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;

Getting php to properly add days to date

I have a invoicing system that generates the next due date after the invoice is paid. My problem though is I want to generate the next invoice based on the date the last one was due, not when it was paid.
I'm familiar with adding days to the current date such as this:
$nextduedate = date('Y-m-d', strtotime("+30 days"));
Lets say the invoice was due on 2016-05-08 but it was paid on 2016-05-12
How would I get the system to add 30 days to my variable $dueDate which is being pulled from the database and set the next invoices due date 30 days from the prior?
Use DateTime():
$dueDate = new DateTimeImmutable('2016-05-08');
$nextInvoice = $dueDate->modify('+30 days');
echo $nextInvoice->format('Y-m-d');
Try this:
$nextduedate = ('Y-m-d', strtotime($duedate. ' + 30 days'));
That will format your date, then add 30 days to the old due date stored in a variable.

How can I echo out a timestamp pulled out from an SQL database table to be in the user's timezone?

I have records that user's create by submitting a form on my website. When they create a record, a timestamp is also created by default to explain when the record was created. From what I'm aware they are all in UTC/GMT Time which is 4 hours off my EST timezone. When I loop through the records to echo them into a table, I'd like for the timestamp's to show the time of the user logged in. Is there easy way of going about this by changing the variable of the timestamp?
In PHP:
date('Y-m-d H:i:s',strtotime('-4 hours',strtotime(TIME_FROM_DB)));
For more about date() & strtotime() see http://php.net/manual/de/
In MYSQL:
SELECT DATE_ADD(time_field, INTERVAL 31 DAY);
or
SELECT CONVERT_TZ('2004-01-01 12:00:00','GMT','MET');
http://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html#function_adddate
hope helps
Using date_default_timezone_set('US/Eastern'); in PHP will convert the time-zone of your code to that timezone.
When you query the database with NOW() in the SQL-query (or use $currentTime = date('Y-m-d H:i:s', time()); in PHP, and insert $currentTime), it'll insert the time of the time-zone defined above.

Get todays midnight Timestamp

So I am having a little trouble getting today's midnight date using the midnight time using the time() function.
It is a little confusing for me when I explain to my peers on the timestamp I want. What I need is, if today is wed-06-Aug-2014 at 9:00pm , then I want the time at wed-06-Aug-2014 00:00.
Any help?
Just use relative date and time formats:
echo date('D-d-M-Y H:i', strtotime('midnight'));
Demo

Categories