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
Related
Based on the definition given on php.net about function date(), I tried to use it in the code like this:
echo"The date is :".date("l, d/m/Y, h:i:s a", time());
But the timestamp doesn't get printed. Why is this? I am unable to understand this part of the function.
To print timestamp just use time()
echo time();
I understood the problem:
I had to written the code properly but since It was not displaying current date and time, I was confused. I set the time zone to Asia/Kolkata. Now its working fine.
Thank you everyone for help.
I'm trying to store some dates in my datebase (MySQL). But I got a strange conversion error:
This is my piece of PHP code:
$fechanac=mysql_real_escape_string(($_POST["fechanac"]));
echo "<h1>{$fechanac}</h1>";
$fechanac=date('Y-m-d', strtotime(str_replace('/', '-', $fechanac)));
echo "<h1>{$fechanac}</h1>";exit();
See the following three example, trying with (01/01/1900, 01/01/1901 and 01/01/1902).
OUTPUT:
01/01/1900
1970-01-01
01/01/1901
1970-01-01
01/01/1902
1902-01-01
Somebody know why happens this? And how to fix it? I need to insert in my DB, possible dates of living persons. Thanks for reading.
Date stored!
$fechanac=mysql_real_escape_string(($_POST["fechanac"]));
echo "<h1>{$fechanac}</h1>";
$datearray=explode("/",$fechanac);
$fechanac="{$datearray[2]}-{$datearray[1]}-{$datearray[0]}";
I know it's and old post, but maybe this late answer can help others:
The DateTime class correctly manages dates older than 1970. So forget the use of date() and evolve to DateTime:
$fechanac=mysql_real_escape_string(($_POST["fechanac"]));
echo "<h1>{$fechanac}</h1>";
$fechanac=(new DateTime($fechanac))->format('Y-m-d');
echo "<h1>{$fechanac}</h1>";exit();
<?php echo invoice_due_date($invoice); ?>
The above code outputs a date that is formatted as dd/mm/yyyy. I am looking for a way to take that date and then add or remove x amount of days from it and then print the result. I'm a novice when it comes to PHP so everything I have tried has failed. Thanks in advance!
Also, this code snippet is from a no longer supported project called "myclientbase" if it helps.
PHP5 has a nice class called DateTime.
You can initialize it from a string like this:
$date = DateTime::createFromFormat('d/m/Y', invoice_due_date($invoice));
Then, since PHP 5.3.0 this class has a method to add time amounts:
$date->add(new DateInterval('P10D'));
This adds 10 days to your date. See http://php.net/manual/en/datetime.add.php.
strtotime can translate the date to a time value (although you'd have to replace the slashes with dashes) and in the same operation add days:
strtotime(str_replace('/','-',invoice_due_date($invoice)) . ' + 1 day')
date can be used to format the date back to a suitable notation.
You can use the DateTime::add function. A full explanation and examples can be found at http://php.net/manual/en/datetime.add.php
$invoice->add(new DateInterval('P20D'));
This would add 20 days to the invoice date. You can simply run this before echoing out your $invoice variable.
I have the following two names in MySQL table:
message is type text
date is type datetime
<?php
while($row=mysql_fetch_array($result)){
?>
<tr>
<td><div><a><? echo $row['message'] ?></a></div></td>
<td><div><a><? echo $row['date']; ?></a></div></td>
</tr>
<?php
}
?>
The output of the above table shows date in this format 2012-08-05 17:43:57
How to print date in this format 2012-08-05 (without time)?
I cannot change type datetime in MySQl (it’s required in different pages in different formats).
I’ve tried the following, but it doesn’t help (no printing at all)
<td><div><a><? echo $rows['date'](“Y-m-d”); ?></a></div></td>
p.s. other combinations of date format give me syntax error.
Any suggestion would be much appreciated,
You want
<?php echo date('Y-m-d', strtotime($row['date'])); ?>
strtotime converts your datestring into a unix-timestamp
then, the date function formats it properly according to the string.
You want the date() to change the format.
There is something called TO_CHAR method in MS-SQL and Oracle in which you can pass the format you want your date in, so while fetching the data you can change it
You should use format function
echo new DateTime($row['date'])->format('Y-m-d');
UPDATED thanks to #Erty
http://php.net/manual/en/datetime.format.php
<td><div><a><? echo new DateTime($row['date'])->format('Y-m-d'); ; ?></a></div></td>
every one else is suggesting you do it with php, i always do this with mySQL's own date function:
SELECT ... DATE_FORMAT(Date_Field ,'%Y-%e-%c') ...
DATE_FORMAT
You first need to convert it to a date:
$tmpdate = strtotime($row['date']);
$date = date('Y-M-d', $tmpdate);
echo $date;
Of course you may short it down, as others suggested, but i prefer "the long way" to improve readability and help you get up to speed faster when you go back and review old code in the future. :-)
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)