strtotime

Check if variable is a valid date with PHP

人盡茶涼 提交于 2019-11-27 20:23:16
I am working on a script that will import some data from a CSV file. As I am doing this I want to be able to check a variable to see if it is a valid date string. I have seen several ways to check if a sting is a date, but most of them require you to now the format. I will not know the format that the date will in. right now I am using strtotime(), but this fails to easily $field ="May"; if(strtotime($field)){ echo "This is a date"; } In this case, "May" was the persons first name, and not a date at all. Can any one recommend more reliable function? Edit based on questions from some of you.

Determine If Business Is Open/Closed Based On Business Hours

柔情痞子 提交于 2019-11-27 18:25:23
My code works fine if the times are AM to PM (Ex: 11 AM - 10 PM), but if the locations hours of operation are AM to AM (Ex: 9 AM - 1 AM) it breaks. Here is my code: $datedivide = explode(" - ", $day['hours']); //$day['hours'] Example 11:00 AM - 10:00 PM $from = ''.$day['days'].' '.$datedivide[0].''; $to = ''.$day['days'].' '.$datedivide[1].''; $date = date('l g:i A'); $date = is_int($date) ? $date : strtotime($date); $from = is_int($from) ? $from : strtotime($from); $to = is_int($to) ? $to : strtotime($to); if (($date > $from) && ($date < $to) && ($dateh != 'Closed')) { ?> <script type="text

PHP date showing '1970-01-01 ' after conversion

◇◆丶佛笑我妖孽 提交于 2019-11-27 18:11:34
I have a form in which date format is dd/mm/yyyy . For searching database , I hanverted the date format to yyyy-mm-dd . But when I echo it, it showing 1970-01-01 . The PHP code is below: $date1 = $_REQUEST['date']; echo date('Y-m-d', strtotime($date1)); Why is it happening? How can I format it to yyyy-mm-dd ? Replace / with - : $date1 = strtr($_REQUEST['date'], '/', '-'); echo date('Y-m-d', strtotime($date1)); January 1, 1970 is the so called Unix epoch. It's the date where they started counting the Unix time . If you get this date as a return value, it usually means that the conversion of

PHP, use strtotime to subtract minutes from a date-time variable?

不羁的心 提交于 2019-11-27 15:57:15
问题 I need to subtract 45 minutes from the date-time variable in PHP. The code: $thestime = '2012-07-27 20:40'; $datetime_from = date("Y-m-d h:i",strtotime("-45 minutes",strtotime($thestime))); echo $datetime_from; returns the result 2012-07-27 07:55 . It should be 2012-07-27 19:55 , though. How do I fix this? 回答1: You should do: $datetime_from = date("Y-m-d H:i", strtotime("-45 minutes", strtotime($thestime))); Having H instead of h means a 24-hour format is used, representing the hour with

Convert this string to timestamp PHP [duplicate]

醉酒当歌 提交于 2019-11-27 14:23:47
This question already has an answer here: Convert one date format into another in PHP 15 answers I have this string: "13/10 15:00" and I would like to convert it to timestamp but when I do this: $timestamp = strtotime("13/10 15:00"); It returns an empty value. In your code strtotime() is attempting to convert 13/10 as the tenth day of the 13th month, which returns an error. If you want to parse a date string with a custom format, it's better to use DateTime::createFromFormat() instead: $dtime = DateTime::createFromFormat("d/m G:i", "13/10 15:00"); $timestamp = $dtime->getTimestamp();

Working days (Mon-Fri) in PHP

最后都变了- 提交于 2019-11-27 13:25:40
Is there a way to use strtotime to add working days (Monday to Friday) to a date? Or some other method? What I want to do is: date ( 'Y-m-j' , strtotime ( '+3 working days' ) ) If you are limiting to weekdays use the string weekdays. echo date ( 'Y-m-j' , strtotime ( '3 weekdays' ) ); This should jump you ahead by 3 weekdays, so if it is Thursday it will add the additional weekend time. Source: http://www.php.net/manual/en/datetime.formats.relative.php I have found this buggy when needing a larger amount of weekdays. I was looking for X amount of business days after the 1st of the current

Finding days between 2 unix timestamps in php

孤街浪徒 提交于 2019-11-27 12:03:25
问题 Hay, i have a database holding events. There are 2 fields 'start' and 'end', these contain timestamps. When an admin enters these dates, they only have the ability to set the day,month,year. So we are only dealing with stamps containing days,months,years, not hours,minutes,seconds (hours,minutes and seconds are set to 0,0,0). I have an event with the start time as 1262304000 and the end time as 1262908800. These convert to Jan 1 2010 and Jan 8 2010. How would i get all the days between these

Adding three months to a date in PHP

拟墨画扇 提交于 2019-11-27 11:59:59
I have a variable called $effectiveDate containing the date 2012-03-26 . I am trying to add three months to this date and have been unsuccessful at it. Here is what I have tried: $effectiveDate = strtotime("+3 months", strtotime($effectiveDate)); and $effectiveDate = strtotime(date("Y-m-d", strtotime($effectiveDate)) . "+3 months"); What am I doing wrong? Neither piece of code worked. Change it to this will give you the expected format: $effectiveDate = date('Y-m-d', strtotime("+3 months", strtotime($effectiveDate))); I assume by "didn't work" you mean that it's giving you a timestamp instead

DateTime class vs. native PHP date-functions

走远了吗. 提交于 2019-11-27 09:24:34
The DateTime class sure has some handy methods and seems overall superior to the native PHP date functions like strtotime , mktime and strftime (and more). But is there any drawback or a reason why I shouldn't use it ? The only reason I can think of is that it might be more expensive to create a whole instance of a class than just using a function. Would you agree with that ? Does it make sense at all to use a DateTime object for simple stuff? Are there any other drawbacks ? It seems a bit confusing to switch between those two options all the time, so I'd like to have clearance what I should

php - add two hours to date variable

感情迁移 提交于 2019-11-27 09:21:30
I want to add 3 minutes to a date/time variable I have, but I'm not sure how to do this. I made the variable from a string like this: (which is in the RFC 2822 date format btw) $date = 2011-10-18T19:56:00+0200 I converted that string into date using this command: $time = date_format(DateTime::createFromFormat("Y-m-d\TH:i:sO", $date), "G:i") Now, I'd like to add 3 minutes to that variable, but I I'm not sure how. I've used the following command in my script before, but that applies to the current date/time, so I'm not sure how to use that for my time variable: $currenttime = date('G:i',