Date to day conversion in php - php

in my webpage i need to calculate the day[ie. SUN,MON,TUE...] from the date .. The date is in ['06/22/2009'] this format ? How can i calculate it in to day[That is it will show me MON] in php . Please help me to find out . Thanks in advance..

First, you need to parse the string '06/22/2009' into a timestamp, possibly using strtotime():
$dt = strtotime('06/22/2009');
Then, you can format the timestamp using date():
$day = date("D", $dt);
If you especially want it in uppercase, use strtoupper():
print strtoupper($day);

For future viewers, I think this will be more helpful.
echo date('l', strtotime('11/20/2017'));

Use the date
http://au.php.net/manual/en/function.date.php
and strtotime http://au.php.net/strtotime
functions

pls check the funcion. the out put is showing the current day.pls tell me the given date dau in the date("D",$dat)

Related

Convert date time to timestamp using php

I am quite sure this has been discussed before but for some reason mine is not working.
I am trying to convert date time to a timestamp.
echo strtotime("18 May 2.50pm");
The above code returns blank.
Any help is highly appreciated. Thanks in advance.
Use php DateTime::createFromFormat to make DateTime
<?php
$date = DateTime::createFromFormat('j M h.ia', '18 May 2.50pm');
echo $date->getTimestamp();
//print_r($date);
?>
Live Demo
Now you can use method available for DateTime .
Note : As you have not given year it will give you result for current year

Need to reformat date variable to match date function in mysql

I have searched high and low on how to do this.
I am using the following datepicker script. http://jqueryui.com/datepicker/
Unfortunately it formats the date like so xx/xx/xxxx. However, the date function in mysql saves the date like so xxxx-xx-xx. I used str_replace to replace the / to -. My deli-ma is trying to figure out now how to convert my variable $editsentdate from xx-xx-xxxx to the mysql date format xxxx-xx-xx. When I just send the variable to my DB like it is it stores it as 0000-00-00 which I am sure is b/c that it is not formatted by Ymd. Any help would be appreciated.
$editsentdate = str_replace('/','-',$_POST['edit_sent_date']); // xx-xx-xxxx
Use PHP's function date(). Example: date("Y-m-d", strtotime($_POST['edit_sent_date']))
$editsentdate = date("Y-m-d",strtotime($_POST['edit_sent_date']));
In datepicker, you can format the date
$('#datepicker').datepicker({ dateFormat: 'yy-mm-dd' });
More info here
There is also a createFromFormat function in PHP. You could initiliaze the date and then format it any way you want.
See PHP - DateTime createFromFormat
<?php
$date = DateTime::createFromFormat('j-M-Y', '15-Feb-2009');
echo $date->format('Y-m-d');
?>
$mysql_date = preg_replace('~^([0-9]{2})/([0-9]{2})/([0-9]{4})$~',
'$3-$2-$1', $_POST['edit_sent_date']);
Assuming the date format is reliable. Other answers might be more reliable but this is a different way... just FYI.

convert one date format to another

I need help to convert a formatted date format as stated below:
How can I convert 2010-02-06 14:44:43 to dd/mm/yyyy
Thanks
Great place to start for this type of general information, php docs, specifically related to your date format question: http://us3.php.net/manual/en/function.date.php
-- Edit --
Answer with substr() forced me to do the work... using substr() to format a date is not the best option as PHP has built-in functions for that.
var_dump(date('d/m/Y', strtotime('2010-02-06 14:44:43')));
$date = new DateTime('2010-02-06 14:44:43');
echo $date->format('d/m/Y');
Output:-
06/02/2010
See the manual
Try this:
<?php
$time = strtotime('2010-02-06 14:44:43');
echo date('d/m/Y',$time);
?>

Date_format() ? Question help

If I have a MySQL table field of my one date with this format (09-13-2011 11:22:43), as I do for PHP to print with this format (09/13/2011 11:22:43) Sorry my ignorance I searched the php.net site but can not find something about my request, Apologies.
$mysql_date = '09-13-2011 11:22:43';
$date = DateTime::createFromFormat('m-d-Y H:i:s', $mysql_date);
echo $date->format('m/d/Y H:i:s');
Use:
date( "m/d/Y h:i:s", UNIX_TIMESTAMP($fieldname));
If your field is already timestamp use the following:
date( "m/d/Y h:i:s", $fieldname);
I may be wrong in saying this but I don't think theres a standard way in doing so, unless you want to save the date/time as a unix_timestamp. If you do then you can format the time in however you want using the php date() function. If you aren't then you can always use something like str_replace() on the times to get them to the format you want or even use regex if your feeling adventurous
MySQL's DATE columns format is fairly irrelevant. Just use DATE_FORMAT() to convert the date to a string that suits your needs.

Calculate a date with PHP

I am using an html form to get a user inputted date. The structure of the date inputted is: MM/DD/YYYY. I then need to increment the total days by 196 in PHP. Right now, the data is being posted to a php file called Calculate.php. I was looking into altering the data using date (m d Y); in php, but my friend said that probably wont work. Any ideas? Thank you for your time and have a great day! ^_^
Check out strtotime. This will convert your MM/DD/YYYY format in to a numeric value you can then work with.
Use strtotime again to manipulate the date to add the days to it.
Use strftime to re-format it for display.
e.g.
$d = '08/11/2011';
$dAsPOSIX = strtotime($d);
$dPlus196Days = strtotime('+196 day', $dAsPOSIX);
echo strftime('%m/%d/%Y',$dPlus196Days);
DEMO
Using strtotime magic:
strtotime('08/11/2011 +196 days');
$input = $_POST['date'];
echo date('m d Y', strtotime('+196 days', strtotime($input));

Categories