Zend_Date subtraction of two datetime mysql values - zend-framework

i am building a small application to calculate employee attendance.
A user will check in, his check in time will be recorded in a mysql datetime format, for example
check_in_time 2011-12-16 20:27:20
And when he checks out
check_out_time 2011-12-16 20:27:27
I can do it the conventional way by exploding and doing the subtraction, but am sure Zend_Date has a more efficient way of doing it.

Obviously my question was not hard to answer, anyhow this is the answer to get the time diff in seconds, then i can handle seconds normally.
Thanks for the docs #ajreal, i did not look correctly into them.
public function getTimeInSeconds($dateStart, $dateEnd) {
$dateStart = new Zend_Date($dateStart);
$dateEnd = new Zend_Date($dateEnd);
$new = $dateStart->sub($dateEnd);
$timeInSeconds = $new->getTimestamp();
return $timeInSeconds;
}

Related

Format date and add month to it

I'm currently working with embarcadero c++, this is the first time I'm working with it so it's completely new to me.
What I'm trying to achieve is to get the current date, make sure the date has the "dd/MM/yyyy" format. When I'm sure this is the case I want to add a month to the current date.
So let's say the current date is 08/18/2016 this has to be changed to 18/08/2016 and then the end result should be 18/09/2016.
I've found that there is a method for this in embarcardero however I'm not sure how to use this.
currently I've only been able to get the current date like this.
TDateTime currentDate = Date();
I hope someone will be able to help me out here.
I figured it out.
After I've searched some more I found the way to use the IncMonth method on this page.
The example given my problem is as follows:
void __fastcall TForm1::edtMonthsExit(TObject *Sender)
{
TDateTime StartDate = edtStartDate->Text;
int Months = edtMonths->Text.ToInt();
TDateTime NextPeriod = IncMonth(StartDate, Months);
edtNextPeriod->Text = NextPeriod;
}
After looking at I changed my code accordingly to this
TDateTime CurrentDate = Date();
TDateTime EndDate = IncMonth(CurrentDate, 1);
A date object doesn't have a format like "dd/MM/yyyy". A date object is internally simply represented as a number (or possibly some other form of representation that really isn't your problem or responsibility).
So you don't have to check if it's in this format because no date objects will ever be in this format, they simply don't have a format.
You will have to do additions/subtractions on the Date object that the language or library gives you, THEN (optionally) you can format it to a human-readable string so it looks like 18/08/2016 or 18th of August 2016 or whatever other readable format that you choose.
It might be that the TRANSFER of a date between 2 systems is in a similar format, but then formatting the date like that is entirely up to you.
As for how to do that, the link you posted seems like a possible way (or alternatively http://docwiki.embarcadero.com/Libraries/Berlin/en/System.SysUtils.IncMonth), I'm afraid I can't give you an example as I'm not familiar with the tool/language involved, I'm just speaking generically about Date manipulations and they should ALWAYS be on the raw object.

Noda Time Date Comparison

I am new to Noda Time and I basically want to compare if a date has expired or not. In my case I have an object with the date it was created, represented by a LocalDate and the amount of months it's valid as an int, so I wanted to do a simple:
if ( Now > (dateCreated + validMonths) ) expired = true;
But I can't find in the Noda Time documentation the proper way to get the Now Date (they only show how to get the Now Time as SystemClock.Instance.Now) and the proper way to handle time comparisons.
For example if today is January 1st 2015 and the document was created in December 1st 2014, and it was valid for one month, today it expires its one month validity.
I miss methods such as isBefore() and isAfter() to compare dates and times. Simple overloads of the < > operators could also be very helpful.
EDIT:
1 - Sorry, there are < > operators to compare dates.
2 - I solve my problem using this code (not tested yet!):
...
LocalDate dateNow = this.clock.Now.InZone(DateTimeZoneProviders.Tzdb.GetSystemDefault()).LocalDateTime.Date;
LocalDate dateExpiration = DataASO.PlusMonths(validity);
return (dateNow < dateExpiration);
To get the current date, you need to specify which time zone you're in. So given a clock and a time zone, you'd use:
LocalDate today = clock.Now.InZone(zone).Date;
While you can use SystemClock.Instance, it's generally better to inject an IClock into your code, so you can test it easily.
Note that in Noda Time 2.0 this will be simpler, using ZonedClock, where it will just be:
LocalDate today = zonedClock.GetCurrentDate();
... but of course you'll need to create a ZonedClock by combining an IClock and a DateTimeZone. The fundamentals are still the same, it's just a bit more convenient if you're using the same zone in multiple places. For example:
// These are IClock extension methods...
ZonedClock zonedClock = SystemClock.Instance.InTzdbSystemDefaultZone();
// Or...
ZonedClock zonedClock = SystemClock.Instance.InZone(specificZone);

Convert a string into many variables

I want to split a string variable that comes from a gps, like this:
2013-08-13T14:33:29.000Z
into:
year = 2013 month = 08
I searched for a long time.
I've tried many things but haven't had any succeeded in getting anything to work.
Any ideas?
Here is a basic example of how to do this in Python.
This is neither the most efficient nor the cleanest way to do it, but this illustrates how do split strings and such in a manner that is relevant to a beginning Python programmer.
gpsstring = '2013-08-13T14:33:29.000Z'
year = gpsstring.split('T')[0].split('-')[0]
month = gpsstring.split('T')[0].split('-')[1]
day = gpsstring.split('T')[0].split('-')[2]
hour = gpsstring.split('T')[1].split(':')[0]
minute = gpsstring.split('T')[1].split(':')[1]
second = gpsstring.split('T')[1].split(':')[2].split('.')[0]
Essentially each variable is being set by splitting the gpsstring. We know where to split the gpsstring because the data you've provided is a standard timestamp interpreted from the NMEA Timestamp.
Edit - the Timezone info is the end of the string (the 000Z in this case) and can also be grabbed as follows:
timezone = gpsstring.split('T')[1].split(':')[2].split('.')[1]
Make sense?

Set date in single line edit on opening

I'm using PowerBuilder 10.5 and I have two single line edit (SLE) fields - sle_date1 and sle_date2 on my window.
What I need is for those two fields to be filled once I open my program. sle_date2 has to have the value of today (for example - 09.07.13), and sle_date1 has to have the value of (sle_date2-30 days) (example 09.06.13).
So, as I said, once I open my programs both fields would be filled immediately with values of today's date and the date of a month before.
How could I do that? Any advice just to get me going?
You can add some code to populate the edits in the open() event of your window
with a given date that can be today(), you can compute a new date plus / minus a number of days with RelativeDate()
The following code just answers your question (though it could be better to use some editmask controls instead of singlelineedit as it would ease the handle of user's input):
date ld_now, ld_previousmonth
string ls_datefmt
ls_datefmt = "dd.mm.yy"
ld_now = today()
sle_1.text = string(ld_now, ls_datefmt)
ld_previousmonth= RelativeDate(ld_now, -30)
sle_2.text = string(ld_previousmonth, ls_datefmt)
It shows 09.07.13 and 09.06.13 at this time.
first of all you need to open your window. You can to this with put this code in your application open event (let suppose that your window is w_main):
open(w_main)
After that in put this code in your window's open event:
sle_date1.text = string(today())
sle_date2.text = string(RelativeDate(Today(), -30))
I think this solves your problem. Here is a little help for RelativeDate:
http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.help.pocketbuilder_2.0.pkpsref/html/pkpsref/pkpsref662.htm
Best Regards
Gábor

zend_date relative time

i want to make stackoverflow timestamps(X minutes ago, etc). How i can make it using zend_date? I found How to calculate time passed with PHP or Zend_Date? this realisation, but it uses other library. Are there any different ways?
How about Zend_Date::subDate()?
$d1 = new Zend_Date();
$d2 = new Zend_Date($old_date);
$diff = $d1->subDate($d2);
After you've got the difference, you can check out one of these helpers:
Relative time view helper
Zend_View_Helper_Relativetime
Even if you don't want the whole Zym framework, you could still add their TimeSince view helper to your own application.
You can subtract timestamps of two dates to find difference between them in seconds and then use division and modulo to represent it as minutes, days etc.