Getting a future unix timestamp using a specific date and time? - php

I run a business and have been developing a CMS / invoicing on my website.
"Appointment"
12:30 PM Thu Aug 5 2021
Though I can format this any way I like, is it possible at all to get the unix time stamp of that?
The purpose of doing so would be so I can just insert the unix timestamp and be able to sort my appointments by the closest to date.
Thank you.

$timestamp = strtotime('1/1/2021 21:00');
Replace 1/1/2021 21:00 to the desired date.

Related

PHP auto correct date in specific format

In my code users post date in content. Which is may be in different formats the format which is used in majority auto sets in MySQL date time field format i.e. yyyy-mm-dd hh:mm:ss. My problem is most of them have typo mistakes or different formats which my code doesn't pick correctly and it returns date something like this 1970-01-01 05:00:00. I am in deep from this issue. Is there any function that auto corrects the date time even if there is a typo mistake in it and if time is not available it auto adds the time to it?
Here are some examples of different formats I get
30 September 2017 | 09 31 AM
29 September 2017 | 02:30 PM
27/07/2016 | 08:20 PM
19/09/2017| 01:32 PM
14-July-2017 03:31 PM
September 5 2017
April 7 2016 04:55 PM
Here is my current PHP code
$get_date = ""; //Date in text form
$show_dated = strtotime(str_replace('|', '', $get_date);
$get_date = date("Y-m-d H:i:s", $show_dated);
echo $get_date;
Try using date_parse() instead of strtotime(). I did this for a while, and it functioned better. Ultimately my solution was to build a custom parser based on the confused mess of user inputs. I eyeballed 2000 entries to develop a 'gold standard' set of results, and then fine-tuned an algorithm to match until it performed 100% correctly.

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

PHP/MySQL Timezone when working only with Date not Time

My question is more on the concept side.
I'm wondering how to deal with dates only based information. For example, when you have a day to take a vacation or something like that on February 24th and you store on the database as 2015-02-24 00:00:00 right.... how do I deal with conversions in this case?
I already now how to convert timezones, it works with no problem in cases where the user selects a time too. But when I use 00:00:00 in cases only the date matters I'm having problems to figure it out.
For example:
Let's say I setup my vacation to be on the 24th of February and I'm on EST right, and the system is saving all times on EST. It will convert and it will be saved as 2015-02-24 00:00:00
Then somebody on PST time will check it out, and when it convert to them it will be 2015-02-23 21:00:00 ... and it will show my vacation in their point of you it will be on the 23rd.
Is that a correct logic? How should I save on the database the time in that case? Keep it as 00:00:00?
I hope I was able to explain myself.
Thanks
You can save the date in unix timestamp in the database (always save the current time in GMT even if you are in EST).
And when displaying the date, convert it to the user's timezone.

Proper way to search event based on user date (event time stored in GMT)

I save event datetime in db in GMT. How can I show today's events to user?
This particularly is creating issue when date is changed due to timezone conversion.
Example:
User creates an event for 12 Aug 2015 23:45 (this is in his own timezone)
When I save it in db date becomes 13 Aug 2015 (GMT)
Now If I need to show to user all events on 12 Aug. How do I do that? What my queries will be like..
You can use Carbon's copy() function for this.
See the similar question from this thread:
How can I change the timezone of the outputted date in Laravel 4?

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