Domanda

Lets say I have a date and time in the following format: 09:24:24 Mar 07, 2014 PST

Using PHP I want to be able to work out what the date and time will be exactly 3 days from that date. Then use that info to display a count down, for example: Time up in 2 days, 3 hours, 27 mins and 5 secs.

And finally display a message when the time is up.

I have no idea how to approach this, can anyone point my in the right direction?

Thank you.

È stato utile?

Soluzione

Well there's certainly plenty of ways to approach this, and you haven't posted any code of anything that you've already tried, so the best I can do is point you in the right direction.

I'm assuming you're getting this date from the client (09:24:24 Mar 07, 2014 PST) as a string passed to your PHP script:

The first thing you need to do is parse the string so you can pull out the year, month, day, hour, minutes and seconds. So that you can create a DateTime object in PHP with:

$date = new DateTime();
$date->setDate($year, $month, $day);
$date->setTime($hour, $minute, $second);

Then you can use add() to add 3 days like so:

$date->add(new DateInterval("P3D"));

Now you have your date that you can pass back to your client (javascript) to display some sort of a countdown timer (search google for this, you'll get a plethora of results).

$echo $date->format("Y-m-d H:i:s");
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top