Change returning date from Propel in PHP - php

I have a Database and I use Propel to get the data.
Now I have the following problem:
I want to get the date from one Table and add 30 days on it.
So something like this:
$invoicePayDay = $invoice->getCreationDay()->add(30 days)->format('d.m.Y');
I know that I can read the month, day and year separately and can than add 20 days and create a date from it again. but properly there is a better way to do it with Propel like my example?

Return the date as a var than you can do
$date = date('d.m.Y', strtotime('+30 days', strtotime($returnDate)));

Related

Show text after a date has passed

I am trying to get a simple line of text to appear if todays date is after another date.
I can either get it to appear on all pages or none, but I am unable to get it to display based on whether the challenge start date is before or after todays date. I believe it could be a date format issue, but everything I have tried has fallen short.
Here is my code:
Get todays date
$date_now = new dateTime();
Challenge start date
$challengeStartDate = date('dS F Y', strtotime($this->item->start_date));
echo '<!--' . strtotime('1970/1/1 00:00:00 +' . $validity) . '-->';
New text line
if ($challengeStartDate > $date_now) echo "New Text";
date() returns a string. With $challengeStartDate > $date_now it's like comparing if one string is bigger than the other (not sure if your dateTime handles that).
Your approach is otherwise fine. Just use timestamps to compare. time() gets you the time as a Unix timestamp:
$now = time();
if ($now > strtotime($this->item->start_date)) {
// do your thing
}
Something like this is more what you need. Try it out.
I had the very same problem some time ago.
All you need to do is store your local time in a database so it would be saved statically.
Because in your example, both $challengeStartDate and $date_now will change and update simultaneously and you wiill always get the current pc time!
Try storing it in a table or idk maybe sessions would help too.

reset counter for new date in php

In php i have a code like :
$job_code = $request->get("job_code");
$day = date('d');
$month = date('m');
$year = date('y');
$date = $day.$month.$year;
using this i am getting format like : abc-241018
but i want the format like for date 24 oct : abc-241018-001 (001 to how many enteries may add in a day like 090)
second thing this counter should be changed or reset for the next day like on date change like 25 0ct : abc-251018-001 (001 to how many entries may add in a day like 090) .
it should be reset for new date how can i achieve this by php is there any best way to solve this ?? please help me related to this .
You can build a custom service to provide expected value, it can store nearest date it render a code. Then if date.now and previous date are different, you will reset the counter.
date ("F d Y H:i:s.")
that should do the full date and time if you dont want the time remove the h i s. and that should give you the correct format which you have asked for.
have fun

Counting database for previous month across the new year roll over PHP SQL

I have had a custom wordpress plugin that runs a report for the database entries from the previous few months, and it worked all year, but now that the new year has happened, it shows no results because it is counting back a few months but then keeping it for 2016.
For example, I load the page on Jan 1st 2016, and it shows the results for December 2016 instead of 2015 when I am using the following method for getting last month's data.
$today = date("Y-m-d H:i:s");
$month_current_start = date("Y-m-")."1 0:0:0";
$currentmonth = date("m");
$lastmonthnum = $currentmonth - 1;
$last_month_start = date("Y-").$lastmonthnum."-1 0:0:0";
$last_month_end = date("Y-").$lastmonthnum."-31 0:0:0";
so then I have an SQL that says something like
$var = $wpdb->get_var( "SELECT Count(*) FROM `table` WHERE table.timestamp BETWEEN '$last_month_start' AND '$last_month_end' AND table.amount = 1200" );
Any tips on how to fix this query so that it knows that last month is 2015?
I'm no professional so I'd love beginner-style help :-)
I can make it work manually but i'd like the code to work for the next few months automatically (since the actual plugin calculates the last four months).
Easiest way would be to convert last month to a time stamp, and use that for creating the format you have in your DB. An example based on what you have so far would be:
$today = date("Y-m-d H:i:s");
$month_current_start = date("Y-m-")."1 0:0:0";
$ltime = strtotime("-1 month");
$last_month_start = date("Y-m",$ltime)."-1 0:0:0";
$last_month_end = date("Y-m",$ltime)."-31 0:0:0";
What this does, is it makes a timestamp for 1 month ago from when it is called in the line $ltime = strtotime("-1 month"); From there, it makes the format for the first day and last day of the month (assuming there are always 31 days of course ^^) based on the timestamp, which you'll use to provide the year and the month.
PHP Sandbox with the code example and it's output if you'd want to play around with how strtotime works.
In mysql you have the option to use year_month, see this for reference.
Get the current year and month using your code
update your query accordingly
or you can have your sql server do all the work for you by using the date_add function
Update your question with the db server you are using, then I can write the new query for you.

Query to get rows with a UNIX Timestamp of today

In my DB I have UNIX Timestamps as due dates for jobs that need to run. On a given day or a given hour I'd like to know which jobs need to run. I have the following code to query my jobs table:
$job = Model_Job::find('first',
array(
'where' => array(
array('status_id', 1), // active
array('due_date', today)
)))
The last array is just pseudo code of course. I came up with the following code to figure out if a timestamp is today.
date('Ymd', strtotime(Date::forge(time())->get_timestamp())) == date('Ymd', strtotime($job->due_date))
How would I combine that with my query? Is there a better way then looping through the queried array?
Now, this is probably not a good way but it was the first that came to mind.
$today = strtotime('today 00:00');
You can use the date from that snippet and set a WHERE due_date > $today I'm not quite sure how to do that with your database model.
edit: This relies on that you have correct timezone settings in your php configuration.
As sebastian pointed out, this will get all future dates also, but you could simply use the same technique to get the last timestamp of the day
$todaystart = strtotime('today 00:00'));
$todayend = strtotime('today 23:59:59'));
and the use a BETWEEN operator in your SQL-query

Why my comparison of two dates in PHP is not working?

I have removed the desired output and some styling as they dont affect the question. I cant seem to compare the two dates properly, idea is that if currentDate is greater than deadlineDate there will be no output for that route. What i am trying to do is prevent the system from listing routes which are already closed. I dont understand why its so difficult or then i am missing something very basic here.
<?php
$driveDays = mysql_query("SELECT date,routeid from StopDates where routeid='".$row['id']."' ORDER BY date ASC");
while($stopDates = mysql_fetch_array($driveDays)){
$orderDaysBefore = $row['lastOrderDate']; // How many days before the order must be placed.
// Change the date taken from the query to new format
$originalDate=($stopDates['date']);
$newDate = date("d.m", strtotime($originalDate));
// Count the deadline date for the route.
$deadlineDate = strtotime ("-".$orderDaysBefore." days +12 hours", strtotime ($originalDate)) ;
$deadlineDate = Date('d.m.y G:i', $deadlineDate);
//Get current date which is then compared to the deadline date. Idea is that if currentDate is larger than deadlinedate there will be no input.
$currentDate=Date("d.m.y G:i");
//The line below doesnt seem to be working, i have tried mktime and time too but for some reason it just cant compare.
if (strtotime($currentDate) > strtotime($deadlineDate)){
// Output nothing
}
else { ?>
<p>Output stuff here</p>
<?php
}
}
?>
Problem is that for some rows it hides the route and for some it doesnt. I have tried to do this with mktime and time but i cant seem to figure out what the problem is. Most of the guides i see tell to convert dates to unix timestamp format and if i understand correctly thats exactly what i am trying to do here. Im pretty sure my mistake is a simple one.
Strange thing is that for some dates it seems to work like if deadlineDate is over a month old. deadlineDate forms correctly in the d.m.y G:i format.
Solved
I skipped formatting and compared strtotimes
I added:
$currentDate2=strtotime("now");
$deadlineDate2 = strtotime ("-".$orderDaysBefore." days +12 hours", strtotime ($originalDate)) ;
And then i compare $currentDate2 and $deadlineDate2
Skip the formatting and just compare the results from strtotime().

Categories