PDA

View Full Version : Coding with Dates



doof92
June 10th, 2008, 04:16
Hey,

I've got in my code a date in the format of dd/mm/yyyy (eg 10/06/2008). What I want to do is find out the number of days since last Thursday. Simple enough to explain, but I have no idea about how to go about coding it.

Any help would be appreciated.

Thanks

fnixws
June 10th, 2008, 04:23
May help if you specify what language :P

Php its not too hard.
Use Unix Time stamp for last thurs, Use unix time stamp for now. Get the difference. Convert to Weeks, Days, Min, Sec?

SyntheticChaos
June 10th, 2008, 05:24
If you are going to be using PHP, this page should help you. It has all the functions for Date/Time Handling.
http://us.php.net/manual/en/ref.datetime.php

doof92
June 10th, 2008, 06:39
I'm using ASP, but mostly I was looking for a method of achiving it, rather than specific code :p
Is it possible to get the Unix Time stamp with knowing the specific date, which I don't (only that it's the last Thursday from the date)?

fnixws
June 10th, 2008, 06:45
hmm.. explain some more?
"last thursday" is not specific date? Every week it changes?

doof92
June 10th, 2008, 06:52
For example, today is 10/06/2008, which is a Tuesday. Last Thursday was 5/06/2008, so the value I want to get is "5 days"

Giving another example, imagine the date is 27/06/2008, a Friday. The nearest Thursday which has passed would be 26/06/2008, and so the calue I want to get is "1 day".

That made things a bit clearer?
Thanks

fnixws
June 10th, 2008, 08:05
So i assume it has to count the days the last thursday to pass, not specifically the number of days since thursday the 5/6/08 ?

Not sure how youd go about that. I assume ASP has a date function.
Sorry, i know nothing about ASP, and stumble my way through most languages as it is.

EDIT: Maybe these scripts can be of inspiration to you!

Dynash
June 10th, 2008, 08:20
Someone's asked something like this before, the way they did it though, was with a SQL database, took the date from the SQL database, then loaded todays date, then used another table and worked the difference of days out. They also done it with seconds, mins, hours etc.

I don't have any code, as I don't know ASP and I don't really want to know either :p Best to ask on a ASP related forum?

doof92
June 10th, 2008, 08:55
Suppose something like that could work. Maybe could run a cron job every Thursday to set the date into a database? Then would be easy to find the difference.
Thanks for all the help

krakjoe
June 10th, 2008, 08:59
using System;
using System.Collections.Generic;
using System.Text;

namespace DateDifference
{
class Program
{
public static DateTime GetLastThursday( )
{
DateTime Thursday = DateTime.Now;

do
{
Thursday = Thursday.Subtract(TimeSpan.FromDays(1));
}
while (Thursday.DayOfWeek.ToString() != "Thursday");

return Thursday.Subtract(TimeSpan.FromDays(1));
}
static void Main(string[] args)
{
DateTime Thursday = GetLastThursday();
DateTime MyDay = DateTime.Parse("6/10/2008");
TimeSpan Inbetween = MyDay.Subtract(Thursday);

Console.WriteLine(Thursday.ToString());
Console.WriteLine(MyDay.ToString());

Console.WriteLine("Full days inbetween: {0}", Inbetween.Days );

Console.ReadLine();
}
}
}



TimeSpan is your friend, the format of your date might be a problem ...

fnixws
June 10th, 2008, 10:24
hmm.. just had a thought.

Cant you just write a loop to count backwards 1 second at a time and every pass, check time and date, convert it to a string then check if the first part of that string is thursday?
If it is, then convert the seconds to days?

u know what i mean?


heres my "generic code" for all unknown languages


start loop
time = now
seconds = now - 1 sec
now = $now
if left$(3) $now = "Thu" then
result = seconds / 86400
print "its been" result "days since Thursday"
else continue loop


somthing like that

themoose
June 10th, 2008, 11:13
Using PHP, you would use the strtotime function.


echo date("d-M-Y", strtotime("Last Thursday", strtotime("10/14/2008")));

Breaking it down:


$date_timestamp = strtotime("10/14/2008");
$last_thurs_timestamp = strtotime("Last Thursday", $date_timestamp);
$formatted_date = date("d-M-Y", $last_thurs_timestamp);
echo $formatted_date;


Examples:


echo date("d-M-Y", strtotime("Last Thursday", strtotime("10/06/2008"))); // output: 02-Oct-2008
echo date("d-M-Y", strtotime("Last Thursday", strtotime("10/07/2008"))); // output: 02-Oct-2008
echo date("d-M-Y", strtotime("Last Thursday", strtotime("10/14/2008"))); // output: 09-Oct-2008

krakjoe
June 10th, 2008, 11:30
^^ yeah u would, but not like that ...


<?php
printf( "&#37;d days between dates", ( strtotime( "6/10/2008" ) - strtotime( "Last Thursday", strtotime( "6/10/2008" ) ) ) / 86400 );
?> ...

themoose
June 10th, 2008, 11:32
Oh, I didn't read the question properly, I thought he meant Last Thursday before that date. But yeah, still strtotime.

krakjoe
June 10th, 2008, 11:33
Yeah he did, but he wanted to display the amount of days between a given date and the thursday before it ...

themoose
June 10th, 2008, 11:42
Oh.

doof92
June 10th, 2008, 12:47
Thanks for all the help. With a combination of the help from you all, and elsewhere, got a solid solution which works well. Thanks