PHP timer based on date - php

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.

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");

Related

Using Carbon to find the number of date since 00-00-0000

I wanna know how many days the calendar started started. So I do this:
$beginning = Carbon::create(0, 1, 1)->startOfDay(); // is: 0000-01-01 00:00:00
$date = Carbon::now()->->startOfDay(); // is: 2016-05-25
$beginning->diffInDays($date)); // return: 736473
That correct, right?
I wanna make it possible to do the same in reverse. What is the date when the day is 800000? Is there somethign already for this in Carbon what I overlooked?
0000-01-01 is not a real date: there was no year 0! The year before 1 AD was 1 BC.
Generally speaking, using a date before 1582 AD as an epoch (i.e, a starting reference point) is inadvisable. The Gregorian calendar was established in 1582 AD -- it can be extended backwards to form the proleptic Gregorian calendar, but some software may handle this inconsistently.
That all being said, you should be able to use the Carbon->addDays() method to do what you're trying to accomplish here:
$date = $beginning->addDays(800000);

How to get current World Time

The diffrence between 2 dates - current time and mysql time. This can be backdoored when user change his PC time and the code is Bypassing him from the diffrence check.
I've tried to use
mktime()
but its not working when my mysql date is 1451094007 (Sat, 26 Dec 2015 01:40:07 GMT) and real world time is for example 1451108407 (Sat, 26 Dec 2015 05:40:07 GMT) 4 hours later, and minimum difference is 10 hours user can still add some hours on him own PC and bypass time.
How can I get any world time which can't be manipulated?
You get all timezones using in the world store in array then run in loop
foreach($timezonearray as $timezone){
$time = new DateTime($timezone);
echo $time->format('Y-m-d H:i:s');
}
it will print result like this
http://www.timehubzone.com/worldclock
single way
$time = new DateTime('Africa/Abidjan');
echo $time->format('Y-m-d H:i:s');
you can get all time zones here
timezones list
You can use other services such as http://worldclockapi.com/
(see http://worldclockapi.com/api/json/utc/now)
Fetch the result and feed them to variable.
Basically, your application should use the server time where it's hosted (configured with server clock) and time() should have respected the server clock

How to get strtotime() with written time format?

I need to input, 09 hours 20 minutes to strtotime and to get the corresponding timestamp. I tried out, strtotime("09 hours 20 minutes",0) but it gives me time passed in seconds in current day only. ie 33600.
I need to get the exact timestamp. ie time passed in seconds from 1-1-1970 to current day 09:20. Is there anyway? Any help will be appreciated.
Like Wong said in the first comment:
use "today" as offset
strtotime("09 hours 20 minutes", strtotime("today"));

PHP converting date without a year

I have a string like 09-10 which is representative of mm-dd. I need it in a format something like Monday 10th September? The problem is that I do not have a year and I can't have an array containing months and days because I would like to know the day of the week (Mon, Tue, Wed etc.)
Any idea how to do this in PHP, preferably using date() to format the date?
Note: this is not in MySQL...
You can't get the day (Monday, Tuesday etc) without knowing the year.
You can use date('jS F', strtotime('2012-09-10')); to get the day of the month and month, just shove any old year in there. I'd make sure to use a leap year year though to make sure you catch those pesky feb dates properly.
Example: http://codepad.org/JfUeTQlH
So, like this:
$d_m = '09-10';
$my_date = date('jS F', strtotime('2012-'.$d_m));
As it is clear Every 1st day of year is not Sunday, it happens after regular interval So just saying a date without year is not clear about day(Monday,Tuesday...). it will give you number of day in that year i.e. out of 365days. so if you are working within a year its OK, but if it goes beyond it You wont be able to get the day(Monday,Tuesday...).

Using PHP to parse a Google Calendar XML - End date is off by a day

I am trying to make a webpage and have the next three events on a Google Calendar show up on the home page. I have been using this PHP (http://james.cridland.net/code/google-calendar.html) to access my XML feed and format it into HTML.
The problem I'm having is that for some reason a new day starts at 11am. For example if my Google Calendar has an event from 10am on the 20th of December that lasts an hour, my PHP output will show an event that starts at 10am on the 20th which ends at 11am on the 21st. Otherwise it is working fine.
I have set my time to local (New Zealand) time on my Google Calendar account, and in PHP using date_default_timezone_set("Pacific/Auckland");
The horrible line that calculates the finish date is
$gCalDateEnd = date($dateformat, strtotime($ns_gd->when->attributes()->endTime)+date("Z",strtotime($ns_gd->when->attributes()->endTime)));
where $dateformat is a string with the date format.
The Google Calendar XML gives a start and finish time of
2011-12-22T10:00:00.000+13:00
2011-12-23T11:00:00.000+13:00
respectively, and the PHP is calculating a timeframe of 10.00am 22 December 2011 to 2.00pm 23 December 2011.
Whats going on?!?!
This line is indeed horrible:
$gCalDateEnd = date($dateformat, strtotime($ns_gd->when->attributes()->endTime)+
date("Z",strtotime($ns_gd->when->attributes()->endTime)));
strtotime can handle this type of ISO 8601 dates just fine. This code-fragment is probably written under the assumption that strtotime dismisses the timezone and returns the datetime in UTC and therefore the timezone "correction" needs to be calculated manually - that's what the +date("Z", ...) stands for (with "Z" the second parameter - the timestamp - is actually ignored).
So in your example 13 hours are added to your dates. And 10:00 + 13:00 = 23:00 (11 pm) which is still on the same day, but 11:00 + 13:00 = 24:00 (12 am) which is actually 00:00 on a new day.
So the correct way to convert the date is:
$gCalDateEnd = date($dateformat, strtotime($ns_gd->when->attributes()->endTime));
Try the zend framework for google calendar(It worked for me better than reinventing the wheel): http://framework.zend.com/manual/en/zend.gdata.calendar.html (look at the examples, they're quite easy and helpful)

Categories