I am trying to convert an old office excel sheet document data into PHP web based app for searching and other data analysis. Stuck in how to fields called start_date & end_date. This tells me that if project is active or not. Data saved in below format. It compares both dates and return the project status by comparing with current date.
Start_Date End Date Status
1-Jan-16 31-Dec-16 Active or not
I tried with PHP date("Y-m-d") function but month is an issue.
You can use the strtotime() function on your excel derived dates and then convert it to a usable DateTime value using date() like so:
$time_before = strtotime('1-Jan-16');
$date_before = date('Y-m-d', $time_before); // 2016-01-01
$time_after = strtotime('31-Dec-16');
$date_after = date('Y-m-d', $time_after); // 2016-12-31
Then compare the two dates against each other.
Related
I have created the text box as well as the query for database.But in my case I want to fetch the data from input field and store it in database.
What actually happens is when I use the function $_POST['launch_date'] it displays the date that I add in db but when I store this in a variable and convert it to y-m-d format it doesn't give me the answer from the text field like $launch_date=date("Y-m-d",strtotime($_POST['launch_date']));.
when i print the both the above mentioned code in single line like echo $_POST['launch_date'] .$launch_date."<br>"; I get the following results
30/01/2020 1970-01-01.
The first one is from my text box and the second one is from the variable that I have created.
Use DateTime::create_from_format() to specify the format of your dates.
$launch_date = DateTime::create_from_format('m/d/Y', $_POST['launch_date'])->format('Y-m-d');
$_POST['launch_date'] = '30/01/2020';
$date = str_replace('/', '-', $_POST['launch_date']);
$launch_date = date('Y-m-d', strtotime($date));
echo $_POST['launch_date'] ." ".$launch_date."<br>";
Output: 30/01/2020 2020-01-30
The issue here is that the / has confused the format of m/d/Y (instead of d/m/Y). And if the date time is not valid (for instance, the month is greater that 12) is in the American format, you'll get the default time (aka UNIX timestamp 0) ie 1970-1-1.
You can create date from user input by
$launch_date = DateTime::create_from_format('d/m/Y', $_POST['launch_date'])->format('Y-m-d');
This is create a date variable of the form Year-Month-Date.
You can change the format as per your requirement.
Y-m-d is the standard date format for MySql.
If You are using PHP version 5.2 or lower you , you will have to parse the date in to 'd'. 'm' and 'y' . Then you will have to create a new date.
I have a bunch of excel sheets which I will be uploading to the database; with date in the format 05-Sep-2019. Is there a way to make mysql recognize it as a date rather than a string? I can use replace in excel to replace '-' with '/' if '-' doesn't work.
Thank you.
You can use strtotime function of php and format as you want
$date = '05-Sep-2019';
$newdate = date('Y-m-d',strtotime($date)); //2019-09-05
$newdate = date('Y/m/d',strtotime($date)); //2019/09/05
$newdate = date('d/m/Y',strtotime($date)); //05/09/2019
Or in Mysql
Kindly Note that MySQL stores date in YYYY-MM-DD format by default.
One way is to convert all dates into YYYY-MM-DD format so that it will be compatible with MYSQL.
We can format the date column in excel before exporting it to MYSQL by following the below steps:
Select the column which contains the date
Right click and select format cells
Choose date under category(on left-hand side)
Then choose custom under category
Under type insert the format --> YYYY-MM-DD
You will see all the dates in the columns get converted into the format --> YYYY-MM-DD
Then you can import the date into MYSQL safely.
Another way is to keep the column datatype as VARCHAR and use TO_DATE() function to parse the string data into DATE format.
you can convert it into Y-m-d format
$date = date('Y-m-d',strtotime($your_excel_date)) // 2019-08-29
$date = date('Y/m/d',strtotime($your_excel_date)) // 2019/08/29
I have a controller action in which I fetch the currrent date like following:
$fullDate=date("Y-m-d");
$date = strtotime($fullDate);
$viewModel->setVariable("currentDate",$date);
// Here I would like to pass the variable to the View and then compare it with another date ...
With the method above I'm getting this when I do var_dump($currentDate);
int(1463587200)
which is not what I was expecting... I need a date in format like this 2016-05-19 (year-month-day)... How can I do this the valid way???
This strtotime is converting your formatted date into a Unix time-stamp. just remove it.
$fullDate=date("Y-m-d");
$viewModel->setVariable("currentDate",$fullDate);
you'll be able to compare two dates in this format Y-m-d without converting to time-stamp.
I have a website which saves images into a database. I have successfully made a function that calculates the date that an image is added and this value is also saved into the database. I now want to calculate the date two weeks ahead from the addition date. This will show the date that the image file will cease to exist in the database.
I used the function:
$dateofaddedimage= date("d/m/Y");
This calculate thee current date of the addition of the image.
I am aware that there is the strtodate() function, but i don't think it will help.
Does anyone know how to add two weeks onto this function?
Thanks!
Add a number to time(), which is the current time stamp as seconds from the Unix Epoch.
$twoweeks = time() + (2*7*24*60*60);
$thatasdate = date("d/m/Y", $twoweeks) ;
Check out date_add and the PHP DateTime model.
From the php manual page comes this fine example:
<?php
$date = new DateTime('2000-01-01');
$date->add(new DateInterval('P10D'));
echo $date->format('Y-m-d') . "\n";
If you use this on your problem, you'd have
<?php
$dateofaddedimage = new DateTime('now'); //creates DateTime Model of today (and now)
$dateofimagedestroy = new DateTime('now'); //creates DateTime Model of today as well
$dateofimagedestroy->add(new DateInterval('P14D')); // adds 14 Days to the second date
The problem with what you are doing is that date() returns a string formatted to the date - not a proper date.
I would suggest either inserting a datetime into the database such as:
insert into yourTable (timeColumn) values (now());
This will insert the actual date. From there you can use mysql functions to add and subtract from this date.
Or using a timestamp in your code such as:
$uploadedTime=time();
From there you can either use PHP functions to add or subtract dates, or (as it is a timestamp) you can also use mysql functions to calculate what you need inside queries themselves.
I'm trying to use this class to import a large amount of data. Most of the data is being read correctly, however I have two date columns which are giving me problems.
The dates are in the format DD/MM/YYYY and the values returned are one day ahead of those in the spreadsheet. For example, 04/03/2011 00:00 becomes 04/03/2011 02:00
I have tried accessing the data like this:
$data->sheets[$sheet]['cells'][$row][$col];
I have also tried using the raw data:
$data->sheets[$sheet]['cellsInfo'][$row][$col]['raw']
Which returns the date as a unix timestamp but still it is one day ahead of what it should be.
Is there any way I can force the class to return the value of the column as a simple string?
The solution is simple - why don't you just deduct a day from the timestamp, or from the date you fetch?
$wrongDateTimestamp = "1304398800";
$rightDateTimestamp = strtotime("-1 day", $wrongDateTimeStamp); // Or alternatively - $wrongDateTimeStam - 86400
$rightDate = date("d/m/Y", $rightDateTimestamp);
Hope this helps.Shai.